35

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)?

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • 1
    FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Oct 21 '17 at 19:22

9 Answers9

55
System.out.println(d.toDateMidnight().isEqual(e.toDateMidnight()));

or

System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 6
    toDateMidnight() 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. – Stan Feb 04 '14 at 13:10
  • 4
    I use `toLocalDate()`, which I presume has the same problem. To avoid that I guess you could chain `withZone()`, e.g.: `d.toLocalDate().isEqual(e.withZone(d.zone).toLocalDate())`. Note that this is _not_ tested. – Charles Wood Jul 17 '14 at 16:33
  • 2
    This won't work if d and e are different time zones. Charles Wood's answer above is more reliable – Manish Patel May 06 '15 at 19:56
23

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())
Stan
  • 2,151
  • 1
  • 25
  • 33
8
return DateTimeComparator.getDateOnlyInstance().compare(first, second);

Via How to compare two Dates without the time portion?

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
3

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.

Andrii Polunin
  • 1,306
  • 11
  • 15
1

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);
Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
1

I stumbled into this question while looking for a comparison with today. Here's how you can compare a date to today :

date1.toLocalDate().isBeforeNow() // works also with isAfterNow
Ismail H
  • 4,226
  • 2
  • 38
  • 61
0

This is a static method which works for me.

public static boolean isSameDay(DateTime date1, DateTime date2){
    return date1.withTimeAtStartOfDay().isEqual(date2.withTimeAtStartOfDay());
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
BaptisteM
  • 147
  • 1
  • 12
  • I don't see the advantage of the [existing Answer](https://stackoverflow.com/a/21553438/642706) using `toLocalDate`. Except this one fails if the `DateTime` objects have different time zones whereas the other Answer’s approach would not. Whether this is good or bad depends on the business problem. – Basil Bourque Oct 21 '17 at 19:19
-1
DateTimeComparator.getDateOnlyInstance().compare(obj1, obj2);

obj1 and obj2 can be a String, Long, Date(java.util)... For the details see http://www.joda.org/joda-time/apidocs/index.html?org/joda/time/DateTimeComparator.html

  • 1
    Welcome 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](https://stackoverflow.com/help/how-to-answer). – dferenc Jan 07 '18 at 14:12
-5

Write your own method

public boolean checkEqual(DateTime first,DateTime second){
     if(first.<getterforyear> == second.<getterforyear> && first.<getterformonth> == second.<getterformonth> && first.<getterforday> == second.<getterforday>){
         return true;
  }
 return false;
}
Alex555
  • 55
  • 1
  • 8