This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Current Date Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Current Date Comparison" Watch "Current Date Comparison" New topic

Current Date Comparison

rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82
I need to compare the current dates ONLY. Not the milliseconds. Have a look at the code and corresponding output.

1. compareTo
2. equals
3. before

None od these methods are helping me, as they take milliseconds in to account while comparing the date.

Is there a way to compare ONLY the DATE without taking milliseconds in to account.
Gilles Marceau
Ranch Hand

Joined: Feb 17, 2007
Posts: 78

i don't think you could find a comparison method that ignore the
milliseconds part of an Date object. On the other hand, you could code
it, for example, by providing your own Comparator<Date> that ignore the
milliseconds :

class MyDateComparator implements Comparator<Date> {
public int compare(Date d1,Date d2) {
return (int)((d1.getTime()/1000L) - (d2.getTime()/1000L));

SCJP 1.5<br />SCJD 1.6<br />SCBCD in progress...
rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82
Thanks for the response Gilles Marceau.

Yes we can do it.

Use util.Date's toString() method to convert the date to String form and use String's equals() method.

String's equals() method takes only date into account and doesn't consider milliseconds.

Here is the code and output

[ February 18, 2007: Message edited by: rama murthy ]
It is sorta covered in the JavaRanch Style Guide.
subject: Current Date Comparison
Similar Threads
How to get past date ?
Time Difference
Time format in Java
How to Compare two Calendar Objects.
Challenge: Optimum way to randomize 1,000,000 numbers