I have a problem with parsing ZonedDateTime
:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-ddzhh:mm");
ZonedDateTime.parse(s, formatter);
This results in an error:
java.time.format.DateTimeParseException:
Text '2022-05-24UTC12:15' could not be parsed at index 10
whats wrong with this code?
The character
z
should be able to parse"UTC"
(in mostLocale
s) because UTC is considered a time-zone ID and a time-zone name injava.time
. AVV
can parse time-zone ids whilez
can parse time-zone-names according to the JavaDocs ofjava.time.DateTimeFormatter
, here's the relevant part of the docs:That means you can parse it using the character
V
without providing a specificLocale
to yourDateTimeFormatter
. You will have to put two of them (VV
) or you will get a niceIllegalArgumentException
with the following message:If you still want to use
z
, provide aLocale
that considersUTC
an abbreviation of Universal Time Coordinated, the Central European Summer Time is an abbreviation that definitely changes among differentLocale
s, e.g.CEST
MESZ
Other
Locale
s might have different abbreviations, which makes me wonder if yourLocale
actually even has a different one forUTC
.Provide
Locale.ENGLISH
, for example and it should parse successfully.You should provide one anyway because if you don't, the
DateTimeFormatter
will implicitly use the defaultLocale
of your (Java Virtual) machine.So you can try this:
or this:
both should be able to parse an input like
"2022-05-24UTC12:15"
if you useHH
instead ofhh
for hours of day (hh
= 12h format,HH
= 24h format).