且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 8 - 使用ZonedDateTime的DateTimeFormatter和ISO_INSTANT问题

更新时间:2022-04-17 01:45:17

记录 ISO_INSTANT 格式化程序这里 - 这是一个特殊的格式化程序,旨在允许人类可读的Instant形式。因此,此格式化程序适用于 Instant 而不是 ZonedDateTime

The ISO_INSTANT formatter is documented here - "This is a special case formatter intended to allow a human readable form of an Instant". As such, this formatter is intended for use with an Instant not a ZonedDateTime.

格式化时, ISO_INSTANT 可以格式化任何可提供 ChronoField.INSTANT_SECONDS 和 ChronoField.NANO_OF_SECOND Instant ZonedDateTime 都可以提供这两个字段,因此两者都有效:

When formatting, ISO_INSTANT can format any temporal object that can provide ChronoField.INSTANT_SECONDS and ChronoField.NANO_OF_SECOND. Both Instant and ZonedDateTime can provide these two fields, thus both work:

// works with Instant
Instant instant = Instant.now();
System.out.println(DateTimeFormatter.ISO_INSTANT.format(instant));

// works with ZonedDateTime 
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt.format(DateTimeFormatter.ISO_INSTANT));

// example output
2014-09-02T08:05:23.653Z



解析



解析时, ISO_INSTANT 只会产生 ChronoField.INSTANT_SECONDS ChronoField.NANO_OF_SECOND 。可以从这两个字段构建 Instant ,但 ZonedDateTime 需要 ZoneId 以及:

Parsing

When parsing, ISO_INSTANT will only produce ChronoField.INSTANT_SECONDS and ChronoField.NANO_OF_SECOND. An Instant can be built from those two fields, but ZonedDateTime requires a ZoneId as well:

要解析 ZonedDateTime ,时区 ZoneId 存在。时区可以是(a)从字符串解析,或(b)指定格式化程序(使用JDK 8u20):

To parse a ZonedDateTime it is essential that a time-zone ZoneId is present. The time-zone can be (a) parsed from the string, or (b) specified to the formatter (using JDK 8u20):

// option a - parsed from the string
DateTimeFormatter f = DateTimeFormatter.ISO_DATE_TIME;
ZonedDateTime zdt = ZonedDateTime.parse("2014-09-02T08:05:23.653Z", f);

// option b - specified in the formatter - REQUIRES JDK 8u20 !!!
DateTimeFormatter f = DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault());
ZonedDateTime zdt = ZonedDateTime.parse("2014-09-02T08:05:23.653Z", f);

请参阅 ISO_ZONED_DATE_TIME ISO_OFFSET_DATE_TIME ISO_DATE_TIME (这三个中的任何一个都可用于解析 ZonedDateTime 而无需指定 withZone())。

See documentation for ISO_ZONED_DATE_TIME, ISO_OFFSET_DATE_TIME and ISO_DATE_TIME (any of these three can be used to parse a ZonedDateTime without specifying withZone()).

ISO_INSTANT formatter是一种特殊情况格式化程序,设计用于 Instant 。如果您使用 ZonedDateTime ,则应使用其他格式化程序,例如 ISO_DATE_TIME ISO_ZONED_DATE_TIME

The ISO_INSTANT formatter is a special case formatter designed to work with Instant. If you are using a ZonedDateTime you should use a different formatter, such as ISO_DATE_TIME or ISO_ZONED_DATE_TIME.