简体   繁体   中英

Java 8 Date and Time API - parse yyyy-MM-dd'T'HH:mm:ss.SSSZ

I'm trying to parse date in ISO8601 format:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

Am I correct that it is not possible to parse it with any of the default formats defined in java.time.format.DateTimeFormatter ?

For example ISO_OFFSET_DATE_TIME will parse only:

yyyy-MM-dd'T'HH:mm:ss.SSSZZ

Samples:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
2015-04-29T10:15:00.500+0000

yyyy-MM-dd'T'HH:mm:ss.SSSZZ
2015-04-29T10:15:00.500+00:00

BTW:I know I can define my own formatter that is not the issue. Just wanted to ensure that I'm not missing something as the ISODateTimeFormat of Joda is able to parse both:

 org.joda.time.format.DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
 DateTime dateTime = dateTimeFormatter.parseDateTime("2015-04-29T10:15:00.500+0000");

I am not sure this is your expected answer.

Method 1

Parse using Instant

Instant.parse("2015-06-28T10:13:14.743Z");

Method 2

The given input format is equivalent to ISO_DATE_TIME format after removing 'Z' from the given pattern yyyy-MM-dd'T'HH:mm:ss.SSSZ

Then we can parse it using ISO_DATE_TIME

 text = "2015-06-28T10:13:14.743"
 LocalDateTime.parse(text,DateTimeFormatter.ISO_DATE_TIME)

您是正确的,因为它似乎与任何默认格式都不匹配,因此您需要使用java.time.format.DateTimeFormatterBuilder构建格式。

Joda uses ISO8601 formats for default ISODateTimeFormat. So based on the document http://www.w3.org/TR/NOTE-datetime , Joda would not be able to parse your format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM