Date comparison problem

Discussion in 'Java' started by Petra Neumann, Mar 1, 2005.

  1. Hi,

    I have a problem comparing two Date objects:

    I create the dates by parsing from a String like: 2/23/05 2:00 PM
    by calling this function:

    public static Date getDate(String date){
    try{
    df = new SimpleDateFormat("M/d/y h:m a");
    Date myDate = df.parse(date);
    return myDate;
    }catch(Exception e){
    System.out.println("ERROR "+e.getLocalizedMessage());
    return null;
    }
    }

    Each of these dates represents an update. Now in my code I want to check
    if an update has been written to the file twice in which case I would
    only have to use one. I do this by calling:

    //I save the previous update as "lastUpdateDate".
    //then I get the new update from a string read from a file as above
    updateDate = getDate(dateString);

    //now check for errors in the dataset
    if(lastUpdateDate.before(updateDate)){
    ....
    }

    This should catch all errors where the new updateDate was before the
    lastUpdateDate, or is equal to it.
    However, sometimes it seems that the if clause is also true if
    updateDate seems to represent the same date as lastUpdateDate, for
    example as parsed from:
    2/23/05 4:00 PM -> Wed Feb 23 16:00:00 MST 2005
    2/23/05 4:00 PM -> Wed Feb 23 16:00:00 MST 2005

    Any suggestions why this happens?

    Cheers,
    Petra
    Petra Neumann, Mar 1, 2005
    #1
    1. Advertising

  2. In article <d02o0i$p61i$>,
    Petra Neumann <> wrote:

    > Hi,
    >
    > I have a problem comparing two Date objects:
    >
    > I create the dates by parsing from a String like: 2/23/05 2:00 PM
    > by calling this function:
    >
    > public static Date getDate(String date){
    > try{
    > df = new SimpleDateFormat("M/d/y h:m a");
    > Date myDate = df.parse(date);
    > return myDate;
    > }catch(Exception e){
    > System.out.println("ERROR "+e.getLocalizedMessage());
    > return null;
    > }
    > }
    >
    > Each of these dates represents an update. Now in my code I want to check
    > if an update has been written to the file twice in which case I would
    > only have to use one. I do this by calling:
    >
    > //I save the previous update as "lastUpdateDate".
    > //then I get the new update from a string read from a file as above
    > updateDate = getDate(dateString);
    >
    > //now check for errors in the dataset
    > if(lastUpdateDate.before(updateDate)){
    > ...
    > }
    >
    > This should catch all errors where the new updateDate was before the
    > lastUpdateDate, or is equal to it.
    > However, sometimes it seems that the if clause is also true if
    > updateDate seems to represent the same date as lastUpdateDate, for
    > example as parsed from:
    > 2/23/05 4:00 PM -> Wed Feb 23 16:00:00 MST 2005
    > 2/23/05 4:00 PM -> Wed Feb 23 16:00:00 MST 2005
    >
    > Any suggestions why this happens?
    >
    > Cheers,
    > Petra


    You might examine the two values returned by getTime(). That's what
    before() does.

    --
    John
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
    John B. Matthews, Mar 2, 2005
    #2
    1. Advertising

  3. Petra Neumann

    EdUarDo Guest

    I have tested the before method with two equals dates
    and never gets than one is before the other:

    import java.text.SimpleDateFormat;
    import java.util.Date;


    public class TestBeforeDate {

    public TestBeforeDate() {
    super();
    }

    public Date getDate(String date){
    try{
    SimpleDateFormat df = new SimpleDateFormat("M/d/y h:m a");
    Date myDate = df.parse(date);
    return myDate;
    }catch(Exception e){
    System.out.println("ERROR "+e.getLocalizedMessage());
    return null;
    }
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    String sDate1 = "2/23/05 4:00 PM";
    String sDate2 = "2/23/05 4:00 PM";
    TestBeforeDate test = new TestBeforeDate();
    Date date1 = test.getDate(sDate1);
    Date date2 = test.getDate(sDate2);
    if (date1.before(date2)) {
    System.out.println("date1 before date2");
    } else if (date2.before(date1)) {
    System.out.println("date2 before date1");
    } else {
    System.out.println("same date");
    }
    }

    }


    Anyway you could format the dates to an ISO String -> YYYYMMDD:HHmm
    then use the compareTo method.
    EdUarDo, Mar 2, 2005
    #3
  4. Petra Neumann

    Fred Guest

    A quick point about the last suggestion ("you could format the dates to
    an ISO String -> YYYYMMDD: then use the compareTo method"). This would
    undoubtedly work quite well. It does, however, violate the principle
    of creating code that is self-documenting (i.e. code that states what
    it does). Your functional goal is not to see if two strings are
    lexicographically equal -- it is to see if one date comes before
    another date. Keeping this in mind, perhaps you can see why using the
    ..before() method will reflect what your code really is trying to do.
    When folks read it, they will understand now that you are comparing two
    Dates to see if one comes before another.

    -Fred
    Fred, Mar 2, 2005
    #4
  5. Petra Neumann

    EdUarDo Guest

    I'm agree with you. The right way to do what I said would be to declare
    a new class TextDate or ISODate with a String attribute containing the
    date at ISO format, and implement methods to compare dates. But for that there
    is already a java.util.Date class.
    EdUarDo, Mar 4, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    565
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,186
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    492
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    691
    Richard Heathfield
    Nov 8, 2003
  5. Deepu
    Replies:
    1
    Views:
    213
    ccc31807
    Feb 7, 2011
Loading...

Share This Page