I am having an inexplicable problem with the Java Calendar class when I try to compare with dates. I am trying to compare with Calendars and determine if their difference is> 1 day, and do something based on this difference or not. But that does not work.
If I do this with two dates:
String currDate = aCurrentUTCCalendar.getTime().toString(); String localDate = aLocalCalendar.getTime().toString();
I get the following results:
currDate = "Thu Jan 06 05:58:00 MST 2010" localDate = "Tue Jan 05 00:02:00 MST 2010"
It is right.
But if I do this:
long curr = aCurrentUTCCalendar.getTime().getTime(); long local = aLocalCalendar.getTime().getTime();
I get the following results: (in milliseconds from the era)
curr = -125566110120000 local = 1262674920000
Since there is only about 30 hours between them, the values ββdiffer significantly from each other, not to mention the fact that the annoying negative sign.
This causes problems if I do this:
long day = 60 * 60 * 24 * 1000; // 86400000 millis, one day if( local - curr > day ) { // do something }
What happened? Why getTime ()? ToString () is correct, but calls getTime (). GetTime () are very different?
I am using jdk 1.6_06 on WinXP. I cannot update the JDK for various reasons.
source share