开发者

problem while printing date and time in android

开发者 https://www.devze.com 2023-02-15 18:45 出处:网络
I want to print date and in android. Once I run it for first time its getting correct time and date. But When I install in phone and run it its not getting correct. Its giving only same result when I

I want to print date and in android. Once I run it for first time its getting correct time and date. But When I install in phone and run it its not getting correct. Its giving only same result when I have install it. my code is here:

package com.datePrint;

    import java.util.Calendar;
    import java.util.GregorianCalendar;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class DatePrint extends Activity {
        /** Called when the activity is firs开发者_JS百科t created. */

        static Calendar cal = new GregorianCalendar();
        static int hour = cal.get(Calendar.HOUR);
        static int minute = cal.get(Calendar.MINUTE);
        static int second = cal.get(Calendar.SECOND);
        static int year = cal.get(Calendar.YEAR);
        static int month = cal.get(Calendar.MONTH)+1;
        static int day = cal.get(Calendar.DATE);
        static String date = day+"_"+month+"_"+year+"_";
        static String Current_Time = date+ hour + "_" + minute + "_" + second;

        public static String OUTPUT_FILE = "/sdcard/"+Current_Time+".mp4";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView tv = new TextView(this);

            tv.setText(Current_Time);
            setContentView(tv);
        }
    }

can anybody help me to solve this problem thanks in advance


You have declared all variables as static, so first remove it from all.

You should refer and understand the concept of static.


I am sure that the API level is different in your case. I have faced the same issue few time back. When I run on 1.6 device it work fine but not on 2.1 emulator.

try this code.\

java.text.SimpleDateFormat format = new SimpleDateFormat(
            "dd-MM-yyyy");
            SimpleDateFormat sdfDestination = new SimpleDateFormat("E MMM dd");

            java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0,
                    "GMT+5000"));

            TimeZone timeZone = TimeZone.getDefault();
            format.setTimeZone(timeZone);
            format.setCalendar(cal);            
            java.util.Date date = null;
            String tmp ="";
            try {
                date = format.parse(EditProfile.dateOFBirth);
                Log.v("A", "Date Of Birth ..." + date);
                Calendar tmpCal = Calendar.getInstance();
                tmpCal.setTime(date);
                tmp =  sdfDestination.format(date) + " 00:00:00 IST "+tmpCal.get(Calendar.YEAR);
                Log.v("A", "Date Of Birth new Date..." + tmp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


@PM has it for the most part, but didn't explain why and missed one key point: An activity keeps running until something kills it (either by explicit shutdown which you didn;t provide for, by the OS killing it to release resources, or Force Close from the application services panel), so "running" it a second time just reactivates it with the same values still loaded into the static variables. If you force close it, the next activation will recreate it with a new time, which it will then retain until again Force Close-d.

And the missing key point is that you're doing it in onCreate, so even without static variables it will only happen the once. You need to either do it on activate or arrange for exiting to shut down the Activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消