Date comparison problem

P

Petra Neumann

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
 
J

John B. Matthews

Petra Neumann said:
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.
 
E

EdUarDo

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.
 
F

Fred

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
 
E

EdUarDo

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top