开发者

Get Date information in android

How t开发者_Python百科o get the phone's date,month,year in android ?

Calendar c = Calendar.getInstance();


Just fetch all this from the obove declared stuff

Calendar c = Calendar.getInstance();
    int day = c.get(Calendar.DATE);
                int month = c.get(Calendar.MONTH);
                int year = c.get(Calendar.YEAR);

I think this will work.


I have used the above code, and it works fine. However i recently noticed that when i convert the int's to a string to show the date format as UK, the months were always out by one.

i.e Febuary was showing as '01' March as '02' etc.

When reading the documentation on calender, i realised that the months start at 0 and end at 11.

So i had to modify the code as follows to show date as correct UK format..

//Get Current date and set as text
    Calendar c = Calendar.getInstance();
      int day = c.get(Calendar.DATE);
      int month = c.get(Calendar.MONTH);  //0 = JAN / 11 = DEC
      int year = c.get(Calendar.YEAR);

      month=month+1; //Set int to correct month numeral, i.e 0 = JAN therefore set to 1.

      if (month<=9)  { MONTH$ = "0"+month   ;}
      else {MONTH$ = ""+month;               }    //Set month to MM


     DDMMYYYY = ""+day+"/"+MONTH$+"/"+year;    //Put date ints into string DD/MM/YYYY

I Hope this helps anyone else using the above code to get correct date

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜