سؤال

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

I am trying 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top