11

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Chris
  • 839
  • 2
  • 10
  • 33

3 Answers3

12

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'.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • 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. – Chris Feb 02 '16 at 15:12
  • Sorry `Z` should be without single quotes. Also use small `z` as you are separating `hh` & `mm` with `:` in `+01:00`. – user2004685 Feb 02 '16 at 15:28
  • 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. – Chris Feb 02 '16 at 16:36
  • 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. – user2004685 Feb 02 '16 at 17:10
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

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
1

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" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154