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

4

18

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");
Abbotson answered 29/4, 2015 at 8:26 Comment(3)
You can set your own DateFormatterPreceptor
The documentation isn't clear about the difference between Z and ZZ - could you give some sample text that you're trying to parse that doesn't parse with ISO_OFFSET_DATE_TIME? (It looks like it's more like ZZZZZ than ZZ, as it includes a colon.)Northnorthwest
Turns out defining a java.time formatter that can parse both isn't straightforward.Dinette
R
6

You should never be bothered by those annoying date-format.

There has an new library dateparser.

It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly.

With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd'T'HH:mm:ss.SSSZZ:

Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");

All works fine, please enjoy it.

Ralline answered 17/9, 2019 at 2:30 Comment(1)
That library worked like a charm after a lot of struggle.Settee
F
6

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)
Flavin answered 28/6, 2015 at 13:20 Comment(0)
R
6

You should never be bothered by those annoying date-format.

There has an new library dateparser.

It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly.

With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd'T'HH:mm:ss.SSSZZ:

Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");

All works fine, please enjoy it.

Ralline answered 17/9, 2019 at 2:30 Comment(1)
That library worked like a charm after a lot of struggle.Settee
V
4

You are correct in that this does not appear to match any of the default formats, so you would need to build your format with a java.time.format.DateTimeFormatterBuilder.

Ventura answered 29/4, 2015 at 8:31 Comment(1)
As I sad, I know I can define my own format that is able to parse yyyy-MM-dd'T'HH:mm:ss.SSSZ that isn't the issue here. I want a format that is able to parse both "Z" and "ZZ", ideally default one that is supplied in java.time.format.* somewhereAbbotson
R
1

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.

Reynold answered 29/4, 2015 at 8:47 Comment(2)
You can try for yourself Joda is able to parse it.Abbotson
See my answer, of course Joda / java.util.time can create a DateTimeFormatter that is able to parse the given format.Ventura

© 2022 - 2024 — McMap. All rights reserved.