I wanted to compare the current system date to a fix date, but get the wrong result. By trying to find out what's wrong, I ended with the following Java code:
// today, 18th of August 2010
long currSystem = System.currentTimeMillis();
Calendar calToday = Calendar.getInstance();
Calendar calFix = Calendar.getInstance();
long milliToday = calToday.getTimeInMillis();
long milliOlder = calFix.getTimeInMillis();
long deltaOlder = milliOlder - milliToday;
long deltaSystem = currSystem - milliToday;
// 1st of August 2010
calFix.set( 2010, 8, 1 );
long milliChanged1 = calFix.getTimeInMillis();
long deltaChanged1 = milliChanged1 - milliToday;
boolean isAfter1 = calToday.after( calFix );
boolean isBefore1 = calToday.before( calFix );
// 1st of October 2010
calFix.set( 2010, 10, 1 );
long milliChanged2 = calFix.getTimeInMillis();
long deltaChanged2 = milliChanged2 - milliToday;
boolean isAfter2 = calToday.after( calFix );
boolean isBefore2 = calToday.before( calFix );
The results in the debugger are:
currSystem: 1282160371775
deltaChanged1: 1209600009
deltaChanged2: 6480000009
deltaOlder: 9
deltaSystem: -25
isAfter1: false
isAfter2: false
isBefore1: true
isBefore2: true
milliChanged1: 1283369971809
milliChanged2: 1288640371809
milliOlder: 1282160371809
milliToday: 1282160371800
The small differences for deltaOlder and deltaSystems are due to the execution time, that's no problem. But the results of the comparison of today (18th of August) and the first set date (1st of August) is wrong IMO. isAfter1 should be true and isBefore1 should be false; The comparison with the second set date (1st of October) is ok.
Any suggenstion what I'm doing wrong in the simple thing to compare two dates?