3
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss z");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
// 2016-10-10 09:28:45 PDT
String s = zonedDateTime.format(formatter);
// this call fails
ZonedDateTime.parse(s, formatter);

What's wrong with the given snippet, shouldn't formatter.parse(date.format(formatter)) evaluate to the same date ?
Exception :

java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MilliOfSecond=0, MicroOfSecond=0, HourOfAmPm=9, MinuteOfHour=28, NanoOfSecond=0, SecondOfMinute=45},ISO,America/Los_Angeles resolved to 2016-10-10 of type java.time.format.Parsed

1
  • 1
    Please post the exception and the value of string s to be complete. Commented Oct 8, 2016 at 7:31

2 Answers 2

5

Since you have specified hh (lower case h) for the 12 hour clock you have lost the information about whether this is AM / PM so the parse is complaining about that.

Using yyyy-MM-dd hh:mm:ss a Z to include the AM/PM indicator works.

Sign up to request clarification or add additional context in comments.

1 Comment

Or replace "h" by "H". (24-hour-clock)
0
java.time.ZonedDateTime.parse("2016-10-10T09:28:45-07:00");

java.time.LocalDate.parse("2016-10-10");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.