Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to determine if two "Date" are same ?

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I create two java util Date objects, and set them to the same "real day", in JVM they are two different objects. But how do I tell if they actually contain the same "actual real day" ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about trying the equals() method of the Date class?
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the equals method tests equality right down to the millisecond.

I've just been doing some date/time calculations recently and remembered very quickly how much of a pain the standard java implementations are.

If you are ok using other API's then something like DateUtils from apache commons lang may be of use. It contains an "isSameDay" method.

Other than that you will probably need to use the java.util.Calendar to produce a Date with the non relevant fields (hour/minute/second/millisecond) all set to zero. Then you should be able to do an equals comparison on your 2 'day only' dates.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic