本文介绍了ISO 8601日期时间格式,包含“ Z”和“ +0000”的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ISO 8601中的日期时间格式。

I'm playing with date-time format from ISO 8601. I have this pattern:

yyyy-MM-dd'T 'HH:mm:ssZZ'Z'

,输出为:

2015-11-17T00:00:00 + 0000Z

我的问题是输出是否可以,如果有可能包含+0000的日期,并且考虑到Z都具有相同的含义时区偏移量/ id。在此先感谢您的澄清=)

My question is if the output is ok, if is possible to have in a date +0000 and Z taking in account both has the same meaning time zone offset/id. Thanks in advance for clarification =)

推荐答案

否,不行


否, Z 是,因此不应将其与 +00:00 +0000

No, not OK

No, the Z is an offset-from-UTC so it should not be combined redundantly with a numerical offset of +00:00 or +0000.

虽然我无法访问规范,明确指出, Z 必须遵循一天中的时间:

While I do not have access to a paid copy of the ISO 8601 spec, the Wikipedia page clearly states that the Z must follow the time-of-day:


IETF RFC 3339


自由-可用的(ISO 8601的配置文件)定义了 Z 附加到一天中的某个时间:

IETF RFC 3339

The freely-available RFC 3339, a profile of ISO 8601, defines a Z as being attached to a time-of-day:

RFC也声明了正式的,我们应该同时使用 Z 或数字。在ABNF中,斜线(SOLIDUS)表示或(不包括或),而一对方括号表示可选。

The RFC also states with formal ABNF notation that we should use either a Z or a number. In ABNF, the slash (SOLIDUS) means "or" (exclusive ‘or’), while the pair of square brackets means "optional".

time-zone = Z / time-numoffset

time-zone = "Z" / time-numoffset

此外,部分特别建议反对,其中应包含冗余信息。

Furthermore, section 5.4 of the spec specifically recommends against including redundant information.

Java内置的现代 java.time 类使用标准的解析/生成字符串时默认使用格式。请参见。

The modern java.time classes built into Java use the standard 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.


要使用 00:00 创建字符串,请调用 OffsetDateTime :: format 。使用 DateTimeFormatter 和您定义的格式模式生成文本。

To create a string with the 00:00, call OffsetDateTime::format. Generate text using a DateTimeFormatter with a formatting pattern you define.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" ) ;
String output = OffsetDateTime.now( ZoneOffset.UTC ).format( f ) ;



截断


您可能希望截断任何微秒或纳秒。

Truncating

You may want to truncate any microseconds or nanoseconds.

Instant
.now()
.truncatedTo( ChronoUnit.MILLIS )
.toString()


...并且…

OffsetDateTime
.now( ZoneOffset.UTC )
.truncatedTo( ChronoUnit.MILLIS )
.format(
    DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSxxx" )
)


查看此。

这篇关于ISO 8601日期时间格式,包含“ Z”和“ +0000”的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:03