File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Date comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date comparison" Watch "Date comparison" New topic

Date comparison

Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
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)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Java API Documentation
The Java Tutorial
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Date comparison
It's not a secret anymore!