ISO 8601 日期时间格式结合 'Z' 和 '+0000' 的偏移量
ISO 8601 date-time format combining 'Z' and offset of '+0000'
我正在使用 ISO 8601 的日期时间格式。我有这个模式:
"yyyy-MM-dd'T'HH:mm:ssZZ'Z'"
输出为:
"2015-11-17T00:00:00+0000Z"
.
我的问题是输出是否正常,是否可以在日期 +0000 和 Z 中考虑两者具有相同含义的时区 offset/id。提前感谢您的澄清=)
不,不行
不,Z
是 一个 offset-from-UTC,因此它不应与 +00:00
或 [= 的数字偏移量冗余组合18=].
ISO 8601
虽然我无法访问 ISO 8601 spec, the Wikipedia page 的付费副本,但明确指出 Z
必须遵循一天中的时间:
…add a Z directly after the time without a space.
IETF RFC 3339
可免费获得的 RFC 3339 ISO 8601 配置文件将 Z
定义为附加到一天中的时间:
A suffix … applied to a time …
RFC 还正式声明 ABNF notation 我们应该使用 或 Z
或数字。在 ABNF 中,斜杠 (SOLIDUS) 表示“或”(不包括“或”),而一对方括号表示“可选”。
time-numoffset = ("+" / "-") time-hour [[":"] time-minute]
time-zone = "Z" / time-numoffset
此外,规范的section 5.4特别推荐反对,包括冗余信息。
Java
内置于 Java 中的现代 java.time 类 使用标准 ISO 8601 formats by default when parsing/generating strings. See Oracle Tutorial.
正在解析文本输入
与Z
:
Instant instant = Instant.parse( "2019-01-23T12:34:56.123456789Z" ) ;
与+00:00
:
OffsetDateTime odt = OffsetDateTime.parse( "2019-01-23T12:34:56.123456789+00:00" ) ;
生成文本输出
要使用 Z
创建字符串,只需调用 Instant::toString
。
String output = Instant.now().toString() ; // Capture the current moment in UTC, then generate text representing that value in standard ISO 8601 using the `Z` offset-indicator.
2019-05-22T21:00:52.214709Z
要使用 00:00
创建字符串,请调用 OffsetDateTime::format
。使用具有您定义的格式模式的 DateTimeFormatter
生成文本。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" ) ;
String output = OffsetDateTime.now( ZoneOffset.UTC ).format( f ) ;
2019-05-22T21:00:52.319076+00:00
正在截断
您可能想要截断任何微秒或纳秒。
Instant
.now()
.truncatedTo( ChronoUnit.MILLIS )
.toString()
2019-05-22T21:11:28.970Z
……和……
OffsetDateTime
.now( ZoneOffset.UTC )
.truncatedTo( ChronoUnit.MILLIS )
.format(
DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" )
)
2019-05-22T21:11:29.078+00:00
我正在使用 ISO 8601 的日期时间格式。我有这个模式:
"yyyy-MM-dd'T'HH:mm:ssZZ'Z'"
输出为:
"2015-11-17T00:00:00+0000Z"
.
我的问题是输出是否正常,是否可以在日期 +0000 和 Z 中考虑两者具有相同含义的时区 offset/id。提前感谢您的澄清=)
不,不行
不,Z
是 一个 offset-from-UTC,因此它不应与 +00:00
或 [= 的数字偏移量冗余组合18=].
ISO 8601
虽然我无法访问 ISO 8601 spec, the Wikipedia page 的付费副本,但明确指出 Z
必须遵循一天中的时间:
…add a Z directly after the time without a space.
IETF RFC 3339
可免费获得的 RFC 3339 ISO 8601 配置文件将 Z
定义为附加到一天中的时间:
A suffix … applied to a time …
RFC 还正式声明 ABNF notation 我们应该使用 或 Z
或数字。在 ABNF 中,斜杠 (SOLIDUS) 表示“或”(不包括“或”),而一对方括号表示“可选”。
time-numoffset = ("+" / "-") time-hour [[":"] time-minute]
time-zone = "Z" / time-numoffset
此外,规范的section 5.4特别推荐反对,包括冗余信息。
Java
内置于 Java 中的现代 java.time 类 使用标准 ISO 8601 formats by default when parsing/generating strings. See Oracle Tutorial.
正在解析文本输入
与Z
:
Instant instant = Instant.parse( "2019-01-23T12:34:56.123456789Z" ) ;
与+00:00
:
OffsetDateTime odt = OffsetDateTime.parse( "2019-01-23T12:34:56.123456789+00:00" ) ;
生成文本输出
要使用 Z
创建字符串,只需调用 Instant::toString
。
String output = Instant.now().toString() ; // Capture the current moment in UTC, then generate text representing that value in standard ISO 8601 using the `Z` offset-indicator.
2019-05-22T21:00:52.214709Z
要使用 00:00
创建字符串,请调用 OffsetDateTime::format
。使用具有您定义的格式模式的 DateTimeFormatter
生成文本。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" ) ;
String output = OffsetDateTime.now( ZoneOffset.UTC ).format( f ) ;
2019-05-22T21:00:52.319076+00:00
正在截断
您可能想要截断任何微秒或纳秒。
Instant
.now()
.truncatedTo( ChronoUnit.MILLIS )
.toString()
2019-05-22T21:11:28.970Z
……和……
OffsetDateTime
.now( ZoneOffset.UTC )
.truncatedTo( ChronoUnit.MILLIS )
.format(
DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" )
)
2019-05-22T21:11:29.078+00:00