Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two java.util.Dates to see if they are in the same day

Tags:

java

datetime

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 
like image 692
Jason S Avatar asked Mar 25 '10 17:03

Jason S


People also ask

How do you check if two dates are on the same day Java?

Core Java The class Date represents a specific instant in time, with millisecond precision. To find out if two Date objects contain the same day, we need to check if the Year-Month-Day is the same for both objects and discard the time aspect.

How can I compare two date values in Java?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

How do you check if a date is after another date in Java?

The after() method is used to check if a given date is after another given date. Return Value: true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.

How do you compare instant dates?

The comparison rule should be based on the date not time. For example, instant: 2013-01-03T00:00:00Z. instant (compare to): 2013-01-03T15:00:00Z.


2 Answers

Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); boolean sameDay = cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&                   cal1.get(Calendar.YEAR) == cal2.get(Calendar.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).

like image 55
Michael Borgwardt Avatar answered Oct 15 '22 01:10

Michael Borgwardt


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.

like image 27
Binil Thomas Avatar answered Oct 15 '22 02:10

Binil Thomas