Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern "YYYY-MM-dd HH:mm:ss Z" causes parsing exception where Jodatime did not

I am trying parse ZonedDateTimes from Strings in the following format:

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

I had previously done so successfully using Jodatime's DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss Z").

I am trying to replace Jodatime with the Java Time API, and the same pattern 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 proper pattern to use for the format string in the Java Time API?

like image 872
kes Avatar asked Nov 02 '17 20:11

kes


1 Answers

You should replace your 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);
like image 83
Aleh Maksimovich Avatar answered Sep 22 '22 12:09

Aleh Maksimovich