Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current Date Comparison

 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
}
}
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic