我在比较两个 DateTime 对象时遇到问题。
System.out.println(response.getCreationDate().toString()); // will return "2013-12-31T22:59:21.000+01:00", but...
assertThat(response.getCreationDate(), equalTo(new DateTime("2013-12-31T22:59:21+01:00"))); // will throw an assertation error with the following error
Expected: <2013-12-31T22:59:21.000+01:00>
but: was <2013-12-31T22:59:21.000+01:00>
有人知道我在这里错过了什么吗?
顺便说一句,如果您想知道为什么 DateTime 显示在 GMT+1:00 时区,因为这是我希望我的 DateTime 对象在默认情况下所在的时区。
谢谢!
最佳答案
DateTime
从 AbstractInstant
继承了它的 equals
方法。它是这样实现的
public boolean equals(Object readableInstant) {
// must be to fulfil ReadableInstant contract
if (this == readableInstant) {
return true;
}
if (readableInstant instanceof ReadableInstant == false) {
return false;
}
ReadableInstant otherInstant = (ReadableInstant) readableInstant;
return
getMillis() == otherInstant.getMillis() &&
FieldUtils.equals(getChronology(), otherInstant.getChronology());
}
注意最后一行比较时间顺序。您的实例的时间顺序可能不同。
关于java - 比较两个 joda DateTime 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863650/