Joda-Time DateTimeZone 比较不正确
Joda-Time DateTimeZone not comparing correctly
我一直在为一个使用 Joda-Time 的项目进行单元测试,我注意到 DateTimeZone
本应代表相同时区的实例在 equals
.特别是,在像下面这样的单元测试中
// should be the same
DateTimeZone WAT = DateTimeZone.forOffsetHours(1); // GMT+01:00
DateTimeZone LAG = DateTimeZone.forID("Africa/Lagos"); // Lagos
DateTime sample = new DateTime(2017, 11, 15, 10, 50);
DateTime dtzWAT = sample.withZoneRetainFields(WAT);
DateTime dtzLAG = sample.withZoneRetainFields(LAG);
@Test public void compareZones() { assertEquals(WAT, LAG); }
@Test public void compareTimes() { assertEquals(dtzWAT, dtzLAG); }
尽管 Africa/Lagos
和 GMT+01:00
是同一时区,但两个测试都失败了,并且两个 DateTimes
代表同一时刻 - 事实上,下面的两个测试证实了这一点
// both succeed
@Test public void compareStrings() {
assertEquals(dtzWAT.toString(), dtzLAG.toString());
}
@Test public void checkSameTime() {
assertFalse(dtzLAG.isBefore(dtzWAT));
assertFalse(dtzWAT.isBefore(dtzLAG));
}
为什么会这样?为什么这两个时区实例不等价,这附近有没有?任何帮助将不胜感激。
why does this happen
两个DateTimeZone
不相等:一个叫Africa/Lagos
,另一个叫GMT+01:00
。它在 the Javadoc of DateTimeZone.equals
中说
true if equal, based on the ID and all internal rules
Compares this object with the specified object for equality based on the millisecond instant, chronology and time zone.
时区不相等,因此 DateTime
不相等。
也许它们有效地相等,并不意味着它们实际上相等。
请注意 java.util.TimeZone
有 hasSameRules(...)
方法,它是一个独立于相等的概念。
我一直在为一个使用 Joda-Time 的项目进行单元测试,我注意到 DateTimeZone
本应代表相同时区的实例在 equals
.特别是,在像下面这样的单元测试中
// should be the same
DateTimeZone WAT = DateTimeZone.forOffsetHours(1); // GMT+01:00
DateTimeZone LAG = DateTimeZone.forID("Africa/Lagos"); // Lagos
DateTime sample = new DateTime(2017, 11, 15, 10, 50);
DateTime dtzWAT = sample.withZoneRetainFields(WAT);
DateTime dtzLAG = sample.withZoneRetainFields(LAG);
@Test public void compareZones() { assertEquals(WAT, LAG); }
@Test public void compareTimes() { assertEquals(dtzWAT, dtzLAG); }
尽管 Africa/Lagos
和 GMT+01:00
是同一时区,但两个测试都失败了,并且两个 DateTimes
代表同一时刻 - 事实上,下面的两个测试证实了这一点
// both succeed
@Test public void compareStrings() {
assertEquals(dtzWAT.toString(), dtzLAG.toString());
}
@Test public void checkSameTime() {
assertFalse(dtzLAG.isBefore(dtzWAT));
assertFalse(dtzWAT.isBefore(dtzLAG));
}
为什么会这样?为什么这两个时区实例不等价,这附近有没有?任何帮助将不胜感激。
why does this happen
两个DateTimeZone
不相等:一个叫Africa/Lagos
,另一个叫GMT+01:00
。它在 the Javadoc of DateTimeZone.equals
true if equal, based on the ID and all internal rules
Compares this object with the specified object for equality based on the millisecond instant, chronology and time zone.
时区不相等,因此 DateTime
不相等。
也许它们有效地相等,并不意味着它们实际上相等。
请注意 java.util.TimeZone
有 hasSameRules(...)
方法,它是一个独立于相等的概念。