English 中文(简体)
and印日期和时间问题
原标题:problem while printing date and time in android
  • 时间:2011-03-10 06:03:32
  •  标签:
  • android

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 first 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

问题回答

您已宣布所有变量为>:static,因此,首先remove it

你们应当提及并理解静态的概念。

我确信,在你的情况中,APIC水平是不同的。 我面临同样问题的时间不多。 当我用1.6个装置运行时,它会做罚款,但不是在2.1个 em子上。

尝试这项法典。

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.

而缺失的关键点是,你会重新这样做,即便没有固定变量,也只会一度发生。 你们要么在启动,要么安排退出活动。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签