Hi All,
I am working on a Java application which retrieves two dates (of type datetime) from database table. e.g. 2009-05-13 11:11:03.113 and 2009-05-13 11:11:03.257
I am saving these into two variables date1 and date2 of type java.util.Date.
i.e. date1 --> 2009-05-13 11:11:03.113
date2 --> 2009-05-13 11:11:03.257
As seen these two dates are equal but there is difference in time : 113 v/s 257
I use the following function,
boolean bool1 = date2.after(date1);
boolean bool2 = date2.before(date1);
bool1 and bool2 both contain false values (ideally, bool2- false and bool1 - true)
This was with respect to the values obtained from database.
I tried the following code, but created dates in Java code:
[color=blue]
Calendar cal1 = Calendar.getInstance();
cal1.clear();
cal1.set(Calendar.YEAR, 2009);
cal1.set(Calendar.MONTH, 4);
cal1.set(Calendar.DATE, 13);
cal1.set(Calendar.HOUR, 11);
cal1.set(Calendar.MINUTE, 11);
cal1.set(Calendar.SECOND, 03);
cal1.set(Calendar.MILLISECOND, 113);
java.util.Date utilDate1 = cal1.getTime();
// System.out.println(utilDate1);
Calendar cal2 = Calendar.getInstance();
cal2.clear();
cal2.set(Calendar.YEAR, 2009);
cal2.set(Calendar.MONTH, 4);
cal2.set(Calendar.DATE, 13);
cal2.set(Calendar.HOUR, 11);
cal2.set(Calendar.MINUTE, 11);
cal2.set(Calendar.SECOND, 03);
cal2.set(Calendar.MILLISECOND, 257);
java.util.Date utilDate2 = cal2.getTime();
// System.out.println(utilDate2);
if(utilDate1.after(utilDate2))
System.out.println("1 is > than 2");
else
if(utilDate1.before(utilDate2))
System.out.println("1 is < than 2");[/color]
This time the console printed "1 is < than 2", which is correct.
Can someone please suggest what is happening in first case?