比较两个Joda-Time DateTime对象

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

这是我正在测试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)); 

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

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

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

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

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

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

将打印为true

表达式

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

等于

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