简体   繁体   English

使用Joda时间比较两个日期

[英]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. 1日期是从java.util.date对象创建的,另一个是手动制作的。 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 . 然而,测试结果是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() : 你应该使用toLocalDate()

date1.toLocalDate().isEqual(date2.toLocalDate())

This will get rid of the Time part of the DateTime. 这将摆脱DateTime的Time部分。

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. 如果要忽略时间组件(即只想比较日期),可以使用DateMidnight类而不是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. 另请注意,默认情况下,JDK Date和所有Joda-Time即时类(包括DateTime和DateMidnight)都是使用默认时区构造的。 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. 因此,如果您在代码中创建一个要比较的日期,但是从DB中检索另一个可能以UTC格式存储日期的日期,则可能会遇到不一致,假设您不在UTC时区。

As they're DateTime objects, their time parts are also taken into consideration when you're comparing them. 因为它们是DateTime对象,所以当您比较它们时,它们的时间部分也会被考虑在内。 Try setting the time parts of the first date to 0, like: 尝试将第一个日期的时间部分设置为0,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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