Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date comparison

 
Marilyn Monickam
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to find out if today's date is after a particular date.
If my current date is
CD - Wed Mar 19 17:40:13 IST 2003
and the other date I want to compare to is
OD - Wed Mar 19 00:00:00 IST 2003
If I want to treat these two as equals what do i do?
In other words from the date object how can I get only yyyy-MM-dd and compare it with another.(before or after)
Thanks
Marilyn
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using the Date class, it implements the Comparable interface which has a compareTo() method. You can use this method to easily compare two dates. Look at the API docs for more detailsl about how the method works.
HTH
Layne
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, but Marilyn wanted to ignore the effect of the hours/minutes/seconds fields when making this comparison. Simply using compareTo() won't do that. First you must modify each Date to set the hours/minutes/seconds to some consistent value, like 12:00 AM (midnight). Here's a simple way to do this:

Now the Date represents the same date as before, except it's now at 12:00 AM midnight.
[ March 19, 2003: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic