简体   繁体   中英

Comparing two dates using Joda time

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

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

要么

System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));

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())
return DateTimeComparator.getDateOnlyInstance().compare(first, second);

通过如何比较没有时间部分的两个日期?

If you want to ignore time components (ie 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.

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

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

This is a static method which works for me.

public static boolean isSameDay(DateTime date1, DateTime date2){
    return date1.withTimeAtStartOfDay().isEqual(date2.withTimeAtStartOfDay());
}
DateTimeComparator.getDateOnlyInstance().compare(obj1, obj2);

obj1和obj2可以是String,Long,Date(java.util)...有关详细信息,请参阅http://www.joda.org/joda-time/apidocs/index.html?org/joda/time/DateTimeComparator html的

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM