aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Current Date Comparison" Watch "Current Date Comparison" New topic
Author

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
Hello,

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current Date Comparison