Comparing two dates using Joda time

后端 未结 9 1085
春和景丽
春和景丽 2020-12-14 14:47

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

相关标签:
9条回答
  • 2020-12-14 15:47

    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);
    
    0 讨论(0)
  • 2020-12-14 15:51

    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())
    
    0 讨论(0)
  • 2020-12-14 15:51

    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;
    }
    
    0 讨论(0)
提交回复
热议问题