Parsing a date using DateTimeFormatter ofPattern
Asked Answered
P

3

13

I'm trying to parse a string containing a date and time using the java.time.format.DateTimeFormatter (my ultimate goal is to get the date from this string into a java.time.LocalDate).

I keep getting DateTimeParseExceptions when trying to parse the date. Can someone help please?

The date is in the format "2015-07-14T11:42:12.000+01:00".

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
LocalDateTime temp = LocalDateTime.ofInstant(Instant.from(f.parse(this.dateCreated)), 
                 ZoneId.systemDefault());
LocalDate localDate = temp.toLocalDate();

I've tried different variations in the ofPattern, such as trying to escape the T by surrounding it with single quotes (as done above), and doing the same with the . and I've tried escaping both at the same time.

Do the colons need escaping too?

Appreciate any help with this.

Penicillate answered 2/2, 2016 at 14:58 Comment(0)
D
15

It should be

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

//or

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");

instead of

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");

From JAVADoc:

Offset X and x: This formats the offset based on the number of pattern letters. One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'. Two letters outputs the hour and minute, without a colon, such as '+0130'. Three letters outputs the hour and minute, with a colon, such as '+01:30'. Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. Six or more letters throws IllegalArgumentException. Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.

Dehnel answered 2/2, 2016 at 15:5 Comment(4)
Thanks for your reply, but I still get an exception with this, when I run a unit test which passes in the above date to this method. The exception message is "Text '2015-07-14T11:42:07.000+01:00' could not be parsed at index 23". Is this to do with the single quotes you've put around the Z? As I didnt think these were needed.Penicillate
Sorry Z should be without single quotes. Also use small z as you are separating hh & mm with : in +01:00.Dehnel
Ah, thanks. :) I got it to work in the end by using "yyyy-MM-dd'T'HH:mm:ss.SSSXXX". Don't really understand the difference between X, Z and z though. It worked with three Xs but not one X. Also dont get why three Xs worked, since there are more than 3 characters there.Penicillate
You are right to use XXX since it is 11:42:12.000+01:00. I have added the description from JAVADoc in the solution.Dehnel
R
2

Both "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" and "yyyy-MM-dd'T'HH:mm:ss.SSSVV" would work. Note that 5 Zs work but no less

Rubble answered 2/2, 2016 at 18:14 Comment(0)
F
2

tl;dr

OffsetDateTime.parse( "2015-07-14T11:42:12.000+01:00" )  // No pattern needed. 

No formatting pattern needed

Your input string complies with the ISO 8601 standard for date-time formats.

The java.time classes use ISO 8601 formats by default when parsing/generating text. So no need for you to define a formatting pattern.

OffsetDateTime odt = OffsetDateTime.parse( "2015-07-14T11:42:12.000+01:00" ) ;
Fluted answered 14/6, 2022 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.