开发者

Years are incorrect in Android's date classes

I am trying to compare dates and I have found where my code goes wrong, but I don't know why.

I am t开发者_如何学Gorying to compare a date with today's date (using Gregorian calendars only). I have printed out today's date where ever it is mentioned in the code and in one place it magically changes from year 2010 to year 3910 (today's year + 1900).

Does anyone know any reason for this?

//Prints out 2010   
System.out.println("TodaysDate.getYear():\t"+todaysDate.getYear());
//Prints out 2010
System.out.println(todaysDate);

//Getting a year from a string (it is 2010)
todaysDate.setYear(Integer.parseInt(yea));

//Prints out 2010   
System.out.println("TodaysDate.getYear():\t"+todaysDate.getYear());
//Prints out 3910   
System.out.println(todaysDate);


The date is typically stored as "number of years since 1900", so you need to compensate for that. You can see that in the documentation for Date: http://developer.android.com/reference/java/util/Date.html

EDIT: I should mention what I posted as a comment. Jeff Sharkey recommended against the Calendar class (if you're using that too). android.text.format.DateUtils is much more lightweight (you'll see that the phone stalls for a little bit when you first load the Calendar class, especially on old phones). DateUtils is available in Android 1.5 and up.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜