wood burning stoves 2.0*
The moose likes Java in General and the fly likes Comparing Dates, How To? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing Dates, How To?" Watch "Comparing Dates, How To?" New topic
Author

Comparing Dates, How To?

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
I am looking for an easy way to compare dates. I am not worried about the time. I just want to know if a certain day is more than, equal to or less than another day. I have been using the Calendar and Date classes, but can't seem to get the results I am looking for. I have used the Calendar methods before, after and equal. Before using these methods I set all of the time fields to 0. The before and after methods seem to work, but the equals method doesn't. I think the equals method is comparing whether the objects themselves are equal. I have coded some work arounds that work, but they are pretty awkward. There must be an easier way.
Any help will be greatly appreciated.
Thanks,
Warren Bell
warren@clarksnutrition.com
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
Hi,
this might be not the most elegant way, but it worked for me.
assuming you really managed to set the time to 0 i.e. 00:00
you can check equality with getTimeInMills (not sure about the capitalization) which should be equal on equal dates.
Bye,
J.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The equals() method for Date should definitely give you the same result that comparing getTime() would give you. Another method is to use the compareTo() method, which should return 0 if the dates are equal.
When you find two Dates that you think should be equal, but aren't (according to the JVM), print them out to see what values they have. Do they really appear to be exactly equal? Or is there in fact some part of the dates which is different?
Are you using a java.util.Date, or possibly a java.sql.Date?


"I'm not back." - Bill Harding, Twister
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
Actually I am using java.sql.Date cast to java.util.Date in the Calendar setTime() method and java.util.Calendar. Here is a sample of my code.
Calendar compareDate = Calendar.getInstance();
Calendar recordDate = Calendar.getInstance();
compareDate.add(Calendar.DATE, -7);
compareDate.set(Calendar.MILLISECOND, 0);
compareDate.set(Calendar.SECOND, 0);
compareDate.set(Calendar.MINUTE, 0);
compareDate.set(Calendar.HOUR, 0);
while(resultSet.next())
{
recordDate.setTime(resultSet.getDate("dbRecordDate"));
recordDate.set(Calendar.MILLISECOND, 0);
recordDate.set(Calendar.SECOND, 0);
recordDate.set(Calendar.MINUTE, 0);
recordDate.set(Calendar.HOUR, 0);
if(recordDate.after(compareDate))
{
// This seems to work
}
if(recordDate.equals(compareDate))
{
// This does not work
}
if(recordDate.before(compareDate))
{
// This seems to work
}
}
This code is very awkward. I need a simple way of compareing a date in a db record to another date. The date 01-05-03 15:20:59 should equal the date 01-05-03 04:45:03
Thanks,
Warren Bell
warren@clarksnutrition.com
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Under the circumstances it seem you don't even need equals - just rewrite the code a bit:

Of course you may want to know why equals() apparently isn't working, so read on.
Calendar.HOUR refers to a 12-hour value. You haven't set Calendar.AM_PM, so there's still a difference between a Date in the morning and a Date in the afternoon. If you use Calendar.HOUR_OF_DAY instead you don't need AM_PM, since HOUR_OF_DAY is a 24-hour value.
Also you're comparing Calendars rather than Dates. The Calendar class does not override equals() to provide what we might consider a sensible definition. If you look at the API you see Calendar just inherits Object's equals() method, which compares the identities of the objects - the same way == does. Instead, use getTime() to get a java.util.Date representing the calendar's date/time, and use Date's equals() method (which is overridden to compare the values of the Dates, not just their identities):

By the way Java's Date/Calendar API is confusing enough already - it's probably a bad idea to call a variable "compareDate" when it's a Calendar, not a Date.
Actually readability will benefit if you just make a separate method for the time-zeroing code, which is the only part that requires a Calendar anyway:

Then your main code is just something like:

[ January 17, 2004: Message edited by: Jim Yingst ]
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
Thanks a lot for the suggestions. I tend to get off on a certain track and do not think of other ways a problem can be solved. Your suggestions are much simpler. And yes I do need to work on coding, its pretty sloppy.
Thanks,
Warren Bell
 
Don't get me started about those stupid light bulbs.
 
subject: Comparing Dates, How To?