使用Joda时间比较两个日期

时间:2021-01-11 21:33:14

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:

我想比较两个日期,但是我遇到了麻烦。 1日期是从java.util.date对象创建的,另一个是手动制作的。以下代码是一个示例:

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

然而,测试结果是错误的。我猜这是因为时间。如何检查这两个日期是否相等(我的意思是年,月,日期是相同的)?

9 个解决方案

#1


45  

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

or

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

#2


18  

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

#3


6  

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

Via How to compare two Dates without the time portion?

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

#4


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:

如果要忽略时间组件(即,您只想比较日期),可以使用DateMidnight类而不是Date Time。所以你的例子看起来像这样:

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.

另请注意,默认情况下,JDK Date和所有Joda-Time即时类(包括DateTime和DateMidnight)都是使用默认时区构造的。因此,如果您在代码中创建一个日期进行比较,但是从数据库中检索另一个可能以UTC格式存储日期的日期,则可能会遇到不一致,假设您不在UTC时区。

#5


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:

因为它们是DateTime对象,所以当您比较它们时,它们的时间部分也会被考虑在内。尝试将第一个日期的时间部分设置为0,如:

d = d.withTime(0, 0, 0, 0);

#6


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

#7


0  

This is a static method which works for me.

这是一个适合我的静态方法。

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

#8


-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

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

#9


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

#1


45  

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

or

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

#2


18  

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

#3


6  

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

Via How to compare two Dates without the time portion?

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

#4


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:

如果要忽略时间组件(即,您只想比较日期),可以使用DateMidnight类而不是Date Time。所以你的例子看起来像这样:

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.

另请注意,默认情况下,JDK Date和所有Joda-Time即时类(包括DateTime和DateMidnight)都是使用默认时区构造的。因此,如果您在代码中创建一个日期进行比较,但是从数据库中检索另一个可能以UTC格式存储日期的日期,则可能会遇到不一致,假设您不在UTC时区。

#5


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:

因为它们是DateTime对象,所以当您比较它们时,它们的时间部分也会被考虑在内。尝试将第一个日期的时间部分设置为0,如:

d = d.withTime(0, 0, 0, 0);

#6


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

#7


0  

This is a static method which works for me.

这是一个适合我的静态方法。

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

#8


-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

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

#9


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