9 Replies Latest reply: Mar 1, 2007 11:59 AM by 807599 RSS

    Date Compare Fails

    807599
      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
        • 1. Re: Date Compare Fails
          807599
          That can't be right, unless you've changed the timezone or otherwise fiddled with something
          • 2. Re: Date Compare Fails
            807599
            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
            • 3. Re: Date Compare Fails
              807599
              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());
                  }
              }
              • 4. Re: Date Compare Fails
                807599
                Thanks!!! I did the same thing. I think it is because

                I am retrieving one date from Mysql that shows time part as 10:12:50
                The second date is from SQL database and the time part is 12:36:48.000.

                I will try again now.
                • 5. Re: Date Compare Fails
                  doremifasollatido
                  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
                  • 6. Re: Date Compare Fails
                    807599
                    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.
                    • 7. Re: Date Compare Fails
                      807599
                      I thought you read the dates from two databases -- you're not reading them
                      as Strings then parsing them are you?
                      • 8. Re: Date Compare Fails
                        807599
                        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...
                        • 9. Re: Date Compare Fails
                          807599
                          Yes, I will do that next time.

                          Thanks