The behavior of the method confuses me DateTimeFormatter withZonewhen it comes to parsing. According to him, the documentation:
The analysis should consider two different cases. If a zone was parsed directly from the text, possibly due to the use of DateTimeFormatterBuilder.appendZoneId (), this override zone has no effect. If no zone has been analyzed, then this redefinition zone will be included in the analysis result, where it can be used to create moments and date-time.
Based on this and the fact that it DateTimeFormatter.ISO_DATE_TIMEanalyzes time zones, I expect the following two tests to pass.
@Test
public void testNoZoneInInput() {
final ZonedDateTime expected = ZonedDateTime.of(2017, 2, 2, 9, 0, 0, 0, ZoneId.of("UTC"));
final ZonedDateTime actual = ZonedDateTime.parse("2017-02-02T10:00:00", DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC+1")));
Assert.assertTrue("Expected " + expected + ", got " + actual + " instead.", expected.isEqual(actual));
}
@Test
public void testWithZoneInInput() {
final ZonedDateTime expected = ZonedDateTime.of(2017, 2, 2, 9, 0, 0, 0, ZoneId.of("UTC"));
final ZonedDateTime actual = ZonedDateTime.parse("2017-02-02T09:00:00Z", DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC+1")));
Assert.assertTrue("Expected " + expected + ", got " + actual + " instead.", expected.isEqual(actual));
}
But, while the former does, the latter does not:
java.lang.AssertionError: 2017-02-02T09: 00Z [UTC], 2017-02-02T09: 00 + 01: 00 [UTC + 01: 00].
, , , . , , , JDK, OpenJDK . , ?
java 1.8.0_121.