To compare, you're correctly using equals
. But it's returning false
because they're not the same point in time. If you look at their values, you'll see that A
is actually one hour before B
:
A: 2018-11-04T01:00:00.000-05:00, ISOChronology[America/New_York], ms 1541311200000
B: 2018-11-04T02:00:00.000-05:00, ISOChronology[America/New_York], ms 1541314800000
The result of getMillis()
are also different, meaning that those dates are not the same point in time.
That's because new DateTime(2018, 11, 4, 1, 00, 0)
produces a date/time that's still in EDT ("2018-11-04T01:00:00.000-04:00" -> 1 AM in -04:00 offset).
One hour later (plusHours(1)
), you should get 2 AM in offset -04:00, but that's the instant when DST ends in New York: clocks are set 1 hour back to 1 AM, and the offset changes to -05:00 - that's why A
is 1 AM at -05:00.
B
, on the other hand, is already in EST (not in DST anymore), so it's set to 2 AM in -05:00. It's one hour after A
.
When DST ends, we have this strange situation where a local time exists twice (in this case, the times between 1AM and 1:59AM occur twice: in -04:00 offset, and then in -05:00 offset), and Joda-Time by default chooses the offset before the DST changeover (-04:00).
You can override this, though, using withLaterOffsetAtOverlap()
, which will take the offset after DST ends.
// new DateTime creates 2018-11-04T01:00:00.000-04:00
DateTime A = new DateTime(2018, 11, 4, 1, 00, 0)
// adjust to offset after DST ends (2018-11-04T01:00:00.000-05:00)
.withLaterOffsetAtOverlap()
// add 1 hour
.plusHours(1);
DateTime B = new DateTime(2018, 11, 4, 2, 00, 0);
// now A and B are the same instant
System.out.println(A.equals(B)); // true