比较两个Joda-Time DateTime对象

时间:2021-09-06 21:31:01

I am dealing with something very similar to what has been asked here - compare Joda-Time time zones but it does not seem to work for me.

我正在处理与此处提出的问题非常类似的事情 - 比较Joda-Time时区,但它似乎对我不起作用。

Here's a piece of code which I am testing Joda-Time DateTime with -

这是我正在测试Joda-Time DateTime的一段代码 -

 DateTime estDT = new DateTime(DateTimeZone.forID("America/Puerto_Rico")).withMillisOfSecond(0); 
 DateTime londonDT = new DateTime(DateTimeZone.forID("Europe/London")).withMillisOfSecond(0);

    System.out.println("Comparison " + londonDT.isBefore(estDT)); 
    System.out.println("Comparison " + londonDT.isAfter(estDT)); 

Interestingly enough, I get 'false' from both the sysout statements above. Can anyone please explain what will be the right way of doing this comparison?

有趣的是,我从上面的两个sysout语句中得到'false'。任何人都可以解释一下这种比较的正确方法是什么?

2 个解决方案

#1


17  

You're creating two DateTime instances probably representing the same instant in time, but with different time zones. Neither is before or after the other.

您正在创建两个DateTime实例,这些实例可能代表同一时刻,但具有不同的时区。在另一个之前或之后都没有。

The time zone just affects how the date/time methods like getHourOfDay() convert the instant in time.

时区只会影响getHourOfDay()之类的日期/时间方法如何及时转换。

#2


48  

isAfter and isBefore methods compare dates by millis (ignoring time zone).

isAfter和isBefore方法按millis比较日期(忽略时区)。

In your example, the dates have equal millis.

在您的示例中,日期具有相等的毫秒数。

System.out.println(londonDT.getMillis() == estDT.getMillis());  

will print true.

将打印为真。

Expressions

londonDT.isBefore(estDT) 
londonDT.isAfter(estDT)

are equal to

等于

londonDT.getMillis() < estDT.getMillis()  
londonDT.getMillis() > estDT.getMillis()

#1


17  

You're creating two DateTime instances probably representing the same instant in time, but with different time zones. Neither is before or after the other.

您正在创建两个DateTime实例,这些实例可能代表同一时刻,但具有不同的时区。在另一个之前或之后都没有。

The time zone just affects how the date/time methods like getHourOfDay() convert the instant in time.

时区只会影响getHourOfDay()之类的日期/时间方法如何及时转换。

#2


48  

isAfter and isBefore methods compare dates by millis (ignoring time zone).

isAfter和isBefore方法按millis比较日期(忽略时区)。

In your example, the dates have equal millis.

在您的示例中,日期具有相等的毫秒数。

System.out.println(londonDT.getMillis() == estDT.getMillis());  

will print true.

将打印为真。

Expressions

londonDT.isBefore(estDT) 
londonDT.isAfter(estDT)

are equal to

等于

londonDT.getMillis() < estDT.getMillis()  
londonDT.getMillis() > estDT.getMillis()