Comparing two dates using Joda time

为君一笑 提交于 2019-12-29 04:07:20

问题


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


回答1:


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

or

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



回答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())



回答3:


return DateTimeComparator.getDateOnlyInstance().compare(first, second);

Via How to compare two Dates without the time portion?




回答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.




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



回答6:


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



回答7:


This is a static method which works for me.

public static boolean isSameDay(DateTime date1, DateTime date2){
    return date1.withTimeAtStartOfDay().isEqual(date2.withTimeAtStartOfDay());
}



回答8:


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




回答9:


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


来源:https://stackoverflow.com/questions/13764106/comparing-two-dates-using-joda-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!