Albin
Albin

Reputation: 347

unable to parse String to ZonedDateTime

I tried to parse a string of datetime "20201211100938Z+0000" but I am not able to find a right DateTimeFormatter to be used for this. For all the trials, I am getting some kind of parse exceptions. Can you please help with a right DateTimeFormatter to be used in the

ZonedDateTime.parse("20201211100938Z+0000", **formatter**)

Upvotes: 0

Views: 237

Answers (1)

WJS
WJS

Reputation: 40047

Try the following. You need to treat the existing Z in the date as a literal.

ZonedDateTime zdt= ZonedDateTime.parse("20201211100938Z+0000",
        DateTimeFormatter.ofPattern("yyyyMMddHHmmss'Z'Z"));
System.out.println(zdt);
System.out.println(zdt.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss'Z'Z"));

Prints

2020-12-11T10:09:38Z
20201211100938Z+0000

Upvotes: 2

Related Questions