The pattern "YYYY-MM-dd HH: mm: ss Z" throws a parsing exception where Jodatime does not

I am trying to parse ZonedDateTime from Strings in the following format:

2017-08-10 16:48:37 -0500

Earlier, I successfully used Jodatime DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss Z") .

I am trying to replace the Jodatime Java Time API, and the same template no longer works.

 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss Z"); ZonedDateTime.parse("2017-08-10 16:48:37 -0500", dtf); 

results in the following exception:

 java.time.format.DateTimeParseException: Text '2017-08-10 16:48:37 -0500' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {DayOfMonth=10, OffsetSeconds=-18000, WeekBasedYear[WeekFields[SUNDAY,1]]=2017, MonthOfYear=8},ISO resolved to 16:48:37 of type java.time.format.Parsed 

What is the correct template for a format string in the Java Time API?

+5
source share
1 answer

You must replace the YYYY part with uuuu

  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss Z"); ZonedDateTime parsed = ZonedDateTime.parse("2017-08-10 16:48:37 -0500", dtf); 
+6
source

Source: https://habr.com/ru/post/1273091/


All Articles