Date Compare Fails
Hi , I am trying to find the difference between two dates.
One approach I am using is getTime and see which value is higher.
ModTime.getTime();
Now I have 2 dates
Date 1: 2007-02-28 12:36:39 -> getTime() =1172641549000
Date 2: 2007-02-28 10:12:13--> getTime() =1172675533000
Why is my Time at 10 AM is giving a larger value than the 12 PM time? Due to this my program thinks the 10 AM modified record is the latest.
Can you please suggest a better way or correct the solution. The project is on Java 1.3 version
[580 byte] By [
garollousa] at [2007-11-26 19:59:44]

I run this code:
import java.text.*;
import java.util.*;
public class DateExample {
public static void main (String[] args) throws ParseException {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = f.parse("2007-02-28 12:36:39");
Date date2 = f.parse("2007-02-28 10:12:13");
System.out.println("date1.getTime()=" + date1.getTime());
System.out.println("date2.getTime()=" + date2.getTime());
}
}
and get a consistent output:
date1.getTime()=1172694999000
date2.getTime()=1172686333000
Here's the portable version:
import java.text.*;
import java.util.*;
public class DateExample {
public static void main (String[] args) throws ParseException {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date1 = f.parse("2007-02-28 12:36:39");
Date date2 = f.parse("2007-02-28 10:12:13");
System.out.println("date1.getTime()=" + date1.getTime());
System.out.println("date2.getTime()=" + date2.getTime());
}
}
Are you missing the AM/PM indicator? If "12:36:48" is AM and "10:12:50" is also AM, then your values make more sense (and give the correct hour difference between the two). However, creating a date from your long values gives me:
Wed Feb 28 00:45:49 EST 2007 // Minutes different from yours
Wed Feb 28 10:12:13 EST 2007
By the way, as has been noted in another thread, if you had posted a small example program, this mistake would have been spotted immediately by forum members.Something to think about before you create another topic...