Issue
Given the following test I would have expected the time to be the same, but it's not.
@Test
public void canGetCorrectOffsetFromFormatterDateParse() {
Instant isoDateInstant = LocalDate.parse("2009-12-31",
DateTimeFormatter.ISO_DATE)
.atStartOfDay(ZoneId.systemDefault())
.toInstant();
System.out.println(isoDateInstant);
assertThat(isoDateInstant).isEqualTo("2009-12-31T05:00:00Z");
Instant customDateInstant = LocalDate.parse("03-31-2020",
DateTimeFormatter.ofPattern("MM-dd-uuuu")
.withResolverStyle(ResolverStyle.STRICT))
.atStartOfDay(ZoneId.systemDefault())
.toInstant();
System.out.println(customDateInstant);
assertThat(customDateInstant).isEqualTo("2020-03-31T05:00:00Z");
}
This test fails with the assertion of the customeDateInstance
Expected :2020-03-31T05:00:00Z
Actual :2020-03-31T04:00:00Z
Q: Why are the times that are being appended not the same, and how do I standardize the time portion going forward?
These tests were run in VA, USA, which is the America/New_York ZoneId.
Solution
Because of DST, America/New_York
is GMT-5 in "winter" and GMT-4 in "summer".
In 2020:
- "winter" to "summer" was on Sunday, March 8, 2:00 am,
- "summer" to "winter" was on Sunday, November 1, 2:00 am.
As expected, your code then:
- gives
2020-03-05T05:00:00Z
with03-05-2020
("winter"), - gives
2020-03-15T04:00:00Z
with03-15-2020
("summer").
Answered By - sp00m
Answer Checked By - David Goodson (JavaFixing Volunteer)