• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Current Date Comparison

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic