我正在处理的问题与这里所问的非常相似 - compare Joda-Time time zones但这似乎对我不起作用。
这是我正在测试的一段代码 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 语句中都得到了“错误”。谁能解释一下进行这种比较的正确方法是什么?
最佳答案
isAfter和 isBefore方法按毫秒比较日期(忽略时区)。
在您的示例中,日期的毫秒数相等。
System.out.println(londonDT.getMillis() == estDT.getMillis());
将打印 true
。
表达式
londonDT.isBefore(estDT)
londonDT.isAfter(estDT)
等于
londonDT.getMillis() < estDT.getMillis()
londonDT.getMillis() > estDT.getMillis()
关于java - 比较两个 Joda-Time DateTime 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17172782/