Comparing two dates using Joda time
Solution 1
System.out.println(d.toDateMidnight().isEqual(e.toDateMidnight()));
or
System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));
Solution 2
You should use toLocalDate():
date1.toLocalDate().isEqual(date2.toLocalDate())
This will get rid of the Time part of the DateTime.
There is another approach, but it does not account for the case where the two dates have a different timezone, so it's less reliable:
date1.withTimeAtStartOfDay().isEqual(date2.withTimeAtStartOfDay())
Solution 3
return DateTimeComparator.getDateOnlyInstance().compare(first, second);
Via How to compare two Dates without the time portion?
Solution 4
If you want to ignore time components (i.e. you want to compare only dates) you can use DateMidnight class instead of Date Time. So your example will look something like this:
Date ds = new Date();
DateMidnight d = new DateMidnight(ds);
DateMidnight e = new DateMidnight(2012, 12, 7);
System.out.println(d.isEqual(e));
But beware, it will print "true" only today :)
Also note that by default JDK Date and all Joda-Time instant classes (DateTime and DateMidnight included) are constructed using default timezone. So if you create one date to compare in code, but retrieve another one from the DB which probably stores dates in UTC you may encounter inconsistencies assuming you are not in UTC time zone.
Solution 5
As they're DateTime objects, their time parts are also taken into consideration when you're comparing them. Try setting the time parts of the first date to 0, like:
d = d.withTime(0, 0, 0, 0);
![Marc Rasmussen](https://i.stack.imgur.com/eGInh.png?s=256&g=1)
Comments
-
Marc Rasmussen almost 4 years
I want to compare two dates, however I'm running into trouble. 1 date is created from a
java.util.date
object and the other is manually crafted. The following code is an example:Date ds = new Date(); DateTime d = new DateTime(ds); DateTime e = new DateTime(2012,12,07, 0, 0); System.out.println(d.isEqual(e));
However the test turns out
false
. I am guessing that it is because of the time. How can I check if these two dates are equal to each other (I mean the Year, month, date are identical)? -
Hiery Nomus over 11 yearsThe types are the same, read the question carefully. Both are DateTime.
-
Stan over 10 yearstoDateMidnight() is deprecated and should not be used. Instead, withTimeAtStartOfDay() can be used, but it's still not optimal because it does not remove the timezone, so if the two dates have different timezones, they will never be equal.
-
Charles Wood almost 10 yearsI use
toLocalDate()
, which I presume has the same problem. To avoid that I guess you could chainwithZone()
, e.g.:d.toLocalDate().isEqual(e.withZone(d.zone).toLocalDate())
. Note that this is not tested. -
Charles Wood almost 10 yearsThis is completely unnecessary as Joda Time has a number of utility methods that do exactly this sort of thing.
-
Manish Patel about 9 yearsThis won't work if d and e are different time zones. Charles Wood's answer above is more reliable
-
Andy Hayden over 8 yearsDateMidnight in package time is deprecated suggestion is to use localDate
-
Basil Bourque over 6 yearsI don't see the advantage of the existing Answer using
toLocalDate
. Except this one fails if theDateTime
objects have different time zones whereas the other Answer’s approach would not. Whether this is good or bad depends on the business problem. -
dferenc over 6 yearsWelcome to Stack Overflow! Before answering a question, always read the existing answers. This answer has already been provided. Instead of repeating the answer, vote up the existing answer. Some guidelines for writing good answers can be found here.
-
Basil Bourque over 6 yearsAll the midnight-related classes and methods and methods are deprecated, supplanted by the "first moment of the day" concept and methods.
-
Basil Bourque almost 6 yearsThe Question asked about equality, not before or after. What is
date1
? Not taken from the Question.