Zil*_* av 2 time iso8601 datetime-format timezone-offset
我正在使用ISO 8601中的日期时间格式。我有以下模式:
"yyyy-MM-dd'T'HH:mm:ssZZ'Z'"
输出为:
"2015-11-17T00:00:00+0000Z"。
我的问题是,输出是否正常,如果考虑到日期+0000和Z都可能具有相同的含义,则表示时区偏移量/ id。在此先感谢您的澄清=)
不可以,Z 是一个UTC偏移量,因此不应将其与数字偏移量+00:00或冗余组合+0000。
虽然我无法使用ISO 8601规范的付费副本,但Wikipedia页上明确指出Z必须遵循一天中的时间:
…在没有空格的时间之后直接添加Z。
免费提供的RFC 3339(ISO 8601的配置文件)将a定义Z为附加在一天中:
后缀…应用于时间…
RFC还与正式规定ABNF符号,我们应该使用任何一个Z或一个数字。在ABNF中,斜杠(SOLIDUS)表示“或”(不包括“或”),而一对方括号表示“可选”。
time-numoffset =(“ +” /“-”)time-hour [[“:”] time-minute]
时区=“ Z” /时间数偏移
此外,规范的5.4节特别建议不要包含冗余信息。
解析/生成字符串时,默认情况下,内置于Java中的现代java.time类使用标准ISO 8601格式。请参见Oracle教程。
与Z:
Instant instant = Instant.parse( "2019-01-23T12:34:56.123456789Z" ) ;
Run Code Online (Sandbox Code Playgroud)
与+00:00:
OffsetDateTime odt = OffsetDateTime.parse( "2019-01-23T12:34:56.123456789+00:00" ) ;
Run Code Online (Sandbox Code Playgroud)
要使用创建字符串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.
Run Code Online (Sandbox Code Playgroud)
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 ) ;
Run Code Online (Sandbox Code Playgroud)
2019-05-22T21:00:52.319076 + 00:00
您可能想要截断任何微秒或纳秒。
Instant
.now()
.truncatedTo( ChronoUnit.MILLIS )
.toString()
Run Code Online (Sandbox Code Playgroud)
2019-05-22T21:11:28.970Z
…和…
OffsetDateTime
.now( ZoneOffset.UTC )
.truncatedTo( ChronoUnit.MILLIS )
.format(
DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" )
)
Run Code Online (Sandbox Code Playgroud)
2019-05-22T21:11:29.078 + 00:00