I'm having an inexplicable problem with the Java Calendar class when I'm trying to compare to dates. I'm trying to compare to Calendars and determine if their difference is > than 1 day, and do things bases on that difference or not. But it doesn't work.
If I do this with the two dates:
String currDate = aCurrentUTCCalendar.getTime().toString();
String localDate = aLocalCalendar.getTime().toString();
I get these results:
currDate = "Thu Jan 06 05:58:00 MST 2010"
localDate = "Tue Jan 05 00:02:00 MST 2010"
This is correct.
But if I do this:
long curr = aCurrentUTCCalendar.getTime().getTime();
long local = aLocalCalendar.getTime().getTime();
I get these results: ( in milliseconds since the epoch )
curr = -125566110120000
local = 1262674920000
Since there is only about a 30 hour different between the two, the magnitudes are vastly different, not to mention that 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's wrong? Why are the getTime().toString() calls correct, but the getTime().getTime() calls are vastly different?
I'm using jdk 1.6_06 on WinXP. I can't upgrade the JDK for various reasons.
Your currDate is "B.C.E. 2010-01-06" (Before Common Era), which is 4019 (2x2010 - 1) years ago.
Try this:
public class StrangeDate {
public static void main(String[] args) {
Date d = new Date(-125566110120000L);
System.out.println(d.toString());
//prints 'Thu Jan 06 13:58:00 CET 2010' in my locale
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.YEAR, 4019);
System.out.println(c.getTime().toString());
//prints 'Wed Jan 06 13:58:00 CET 2010' in my locale
}
}
The question is: what did you do to get this aCurrentUTCCalendar
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With