java. time. ZonedDateTime. Parse and iso8601?

Why does the jdk8 datetime library seem unable to resolve valid iso8601 datetime strings? Its time zone offset is expressed as "01" instead of "01:00"

This works:

java.time.zoneddatetime.parse("2015-08-18T00:00+01:00")

This throws a parse exception:

java.time.zoneddatetime.parse("2015-08-18T00:00+01")

From the iso8601 Wikipedia page:

Editor: This looks like an actual legal error in JDK

https://bugs.openjdk.java.net/browse/JDK-8032051

Wow, after years of testing new dates and times, I think they'll catch something so obvious I also think the JDK author type is very strict and can use a better automated test suite

Update: This is fully fixed in the current JDK - 9 release I just confirmed it The exact parsing command shown above failed in the current JDK - 8 build and worked perfectly in JDK - 9

Appendix: fwiw, RFC 3339 based on iso-8601, this abbreviation is not allowed You must specify minutes in the time zone offset

Solution

You can use this default formatter: ISO_ OFFSET_ DATE_ Time (because the resolution 2015-08-18t00:00 01:00)

In the document:

It's (you only use this default formatter):

It seems that Java Time (JDK 8) does not fully implement iso-8601

This:

java.time.zoneddatetime.parse("2015-08-18T00:00+01:00"); // works

Corresponding (roughly from the source JDK):

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder
        .parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        .appendOffsetId()
        .toFormatter();

java.time.zoneddatetime.parse("2015-08-18T00:00+01:00",formatter); // it's same

You can use datetimeformatterbuilder to create your own datatimeformatter

DateTimeFormatterBuilder builder2 = new DateTimeFormatterBuilder();
DateTimeFormatter formatter2 = builder2.parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        .appendPattern("X") // eg.:
        .toFormatter();

java.time.zoneddatetime.parse("2015-08-18T00:00+01",formatter2); // here you set +01

Instead of appendoffsetid(), use appendpattern (string pattern) and set 'x' or 'x'

Now, you can use the data time 2015-08-18t00:00 01

Or... Use the default ISO_ OFFSET_ DATE_ Time and add suffix: 00

java.time.zoneddatetime.parse("2015-08-18T00:00+01" + ":00");

But this is a bad solution in the end

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>