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
zshould be able to parse"UTC"(in mostLocales) because UTC is considered a time-zone ID and a time-zone name injava.time. AVVcan parse time-zone ids whilezcan 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
Vwithout providing a specificLocaleto yourDateTimeFormatter. You will have to put two of them (VV) or you will get a niceIllegalArgumentExceptionwith the following message:If you still want to use
z, provide aLocalethat considersUTCan abbreviation of Universal Time Coordinated, the Central European Summer Time is an abbreviation that definitely changes among differentLocales, e.g.CESTMESZOther
Locales might have different abbreviations, which makes me wonder if yourLocaleactually 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
DateTimeFormatterwill implicitly use the defaultLocaleof 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 useHHinstead ofhhfor hours of day (hh= 12h format,HH= 24h format).