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]
# 1
That can't be right, unless you've changed the timezone or otherwise fiddled with something
tjacobs01a at 2007-7-9 22:56:44 > top of Java-index,Java Essentials,New To Java...
# 2

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

DrLaszloJamfa at 2007-7-9 22:56:44 > top of Java-index,Java Essentials,New To Java...
# 3

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());

}

}

DrLaszloJamfa at 2007-7-9 22:56:44 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks!!! I did the same thing. I think it is becauseI am retrieving one date from Mysql that shows time part as 10:12:50The second date is from SQL database and the time part is 12:36:48.000.I will try again now.
garollousa at 2007-7-9 22:56:44 > top of Java-index,Java Essentials,New To Java...
# 5

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

doremifasollatidoa at 2007-7-9 22:56:44 > top of Java-index,Java Essentials,New To Java...
# 6
Hi Guys,Thanks a lot. The difference in my code is the format of date I used for parsing.I used yyyy-MM-dd hh:mm:ss instead of yyyy-MM-dd HH:mm:ss as posted. I made this change and everything worked fine.
garollousa at 2007-7-9 22:56:44 > top of Java-index,Java Essentials,New To Java...
# 7
I thought you read the dates from two databases -- you're not reading themas Strings then parsing them are you?
DrLaszloJamfa at 2007-7-9 22:56:45 > top of Java-index,Java Essentials,New To Java...
# 8
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...
DrLaszloJamfa at 2007-7-9 22:56:45 > top of Java-index,Java Essentials,New To Java...
# 9
Yes, I will do that next time.Thanks
garollousa at 2007-7-9 22:56:45 > top of Java-index,Java Essentials,New To Java...