这是完整的规范:
* Outputs this date-time as a {@code String}, such as
* {@code 2007-12-03T10:15:30+01:00[Europe/Paris]}.
* <p>
* The format consists of the {@code LocalDateTime} followed by the {@code ZoneOffset}.
* If the {@code ZoneId} is not the same as the offset, then the ID is output.
* The output is compatible with ISO-8601 if the offset and ID are the same.
Javadoc 规范是指 用 a 而不是 named 构造的情况,因此偏移量和 ID 是相同的:ZonedDateTime
ZoneOffset
ZoneId
System.out.println(ZonedDateTime.now(ZoneId.of("Europe/Paris")));
// 2017-04-26T15:13:12.006+02:00[Europe/Paris]
System.out.println(ZonedDateTime.now(ZoneOffset.ofHours(2)));
// 2017-04-26T15:13:12.016+02:00
可以看出,在第二种情况下,使用 a 时,格式省略了末尾的方括号部分。通过省略该部分,结果是ISO-8601兼容。ZoneOffset
toString()
boolean iso8601Compatible = zdt.getZone() instanceof ZoneOffset;
为了保证ISO-8601兼容输出的使用:toOffsetDateTime()
String isoCompatible = zdt.toOffsetDateTime().toString();
或格式化程序。