tags:

views:

693

answers:

5

I need to compare two Dates (e.g. date1 and date2) and come up with a boolean sameDay which is true of the two Dates share the same day, and false if they are not.

How can I do this? There seems to be a whirlwind of confusion here... and I would like to avoid pulling in other dependencies beyond the JDK if at all possible.

to clarify: if date1 and date2 share the same year, month, and day, then sameDay is true, otherwise it is false. I realize this requires knowledge of a timezone... it would be nice to pass in a timezone but I can live with either GMT or local time as long as I know what the behavior is.

again, to clarify:

date1 = 2008 Jun 03 12:56:03
date2 = 2008 Jun 03 12:59:44
  => sameDate = true

date1 = 2009 Jun 03 12:56:03
date2 = 2008 Jun 03 12:59:44
  => sameDate = false

date1 = 2008 Aug 03 12:00:00
date2 = 2008 Jun 03 12:00:00
  => sameDate = false
+2  A: 

Use JodaTime.

Martinho Fernandes
Doesn't this create a new dependency to JodaTime libraries, that is precisely what he wants to avoid? :(
XpiritO
@XpiritO: Yeah, I know. It's the best answer I can think of though.
Martinho Fernandes
A: 

Go to this link. It explains how to do it. But basically you can use the Calendar class.

John
that ignores hour/minute/second
Jason S
take a look at the calendar api. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html You can also set the TimeZone.
John
+8  A: 
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                  cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);

Note that "same day" is not as simple a concept as it sounds when different time zones can be involved. The code above will for both dates compute the day relative to the time zone used by the computer it is running on. If this is not what you need, you have to pass the relevant time zone(s) to the Calendar.getInstance() calls, after you have decided what exactly you mean with "the same day".

And yes, Joda Time's LocalDate would make the whole thing much cleaner and easier (though the same difficulties involving time zones would be present).

Michael Borgwardt
Thanks, that looks like it will do what I want. In my case I'm comparing successive dates in a series, so it looks like I could just use Calendar instances instead of Date instances in my series.
Jason S
@Jason That may or may not be a good idea. The main problem with Calendar is that it is a very heavyweight class with a lot of internal state, some of which is used in its equals() implementation. If you don't copmpare your dates for equality and don't put them into HashMaps, you should be fine.
Michael Borgwardt
cool, thanks, I'm just using the compare-current-and-previous-day logic asked here. the "equals" and "hashcode" functions should never get called.
Jason S
+3  A: 

I use the "apache commons lang" package to do this (namely org.apache.commons.lang.time.DateUtils)

boolean samedate = DateUtils.isSameDay(date1, date2);  //Takes either Calendar or Date objects
Brent Watson
This uses an external dependency... but it's good to know for the future.
Jason S
+4  A: 

How about:

SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
return fmt.format(date1).equals(fmt.format(date2));

You can also set the timezone to the SimpleDateFormat, if needed.

binil
huh. That's kind of clever. thanks!
Jason S
(I'm actually using SimpleDateFormat anyway in my case, so it seems kind of appropriate.)
Jason S