• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing Dates, How To?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic