java - 比较两个 joda DateTime 实例

标签 java datetime jodatime

我在比较两个 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 对象在默认情况下所在的时区。

谢谢!

最佳答案

DateTimeAbstractInstant 继承了它的 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/

相关文章:

java - 实例化和使用枚举单例

java - 如何获取ZZ_CMAP_PACKED中的word boundary?

java - 在maven中,如果我创建一个子包,主包是如何引用的?

java - 如何使用 JodaTime 将本地时间转换为上午/下午时间格式?

java - 是否可以以某种方式扩展标记为最终的 Joda-Time 类?

java - 如果两个复选框的语句被选中问题

c# - 以毫秒 C# 将日期时间插入 SQLCE

c# - 以天为单位的日期范围....C#

java - 如何让 Joda DateTime 解析器只接受 yyyyMMdd 形式的字符串?

mysql - 从存储为 varchar 的时间戳中获取时间