The character z
should be able to parse "UTC"
(in most Locale
s) because UTC is considered a time-zone ID and a time-zone name in java.time
. A VV
can parse time-zone ids while z
can parse time-zone-names according to the JavaDocs of java.time.DateTimeFormatter
, here's the relevant part of the docs:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
(…)
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
(…)
That means you can parse it using the character V
without providing a specific Locale
to your DateTimeFormatter
. You will have to put two of them (VV
) or you will get a nice IllegalArgumentException
with the following message:
java.lang.IllegalArgumentException: Pattern letter count must be 2: V
If you still want to use z
, provide a Locale
that considers UTC
an abbreviation of Universal Time Coordinated, the Central European Summer Time is an abbreviation that definitely changes among different Locale
s, e.g.
- English:
CEST
- German:
MESZ
Other Locale
s might have different abbreviations, which makes me wonder if your Locale
actually even has a different one for UTC
.
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 default Locale
of your (Java Virtual) machine.
So you can try this:
DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu-MM-ddVVHH:mm");
or this:
DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu-MM-ddzHH:mm", Locale.ENGLISH);
both should be able to parse an input like "2022-05-24UTC12:15"
if you use HH
instead of hh
for hours of day (hh
= 12h format, HH
= 24h format).
2022-05-24UTC12:15
— What a weird format. Is there a particular reason why this format is used? – Confuciusjava.time.format.DateTimeParseException: Text '2022-05-24UTC12:15' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MinuteOfHour=15, HourOfAmPm=0},ISO,Etc/UTC resolved to 2022-05-24 of type java.time.format.Parsed
. – RemissibleHH
for hour of day. See for example DateTimeParseException: Text '2019-06-07 12:18:16' could not be parsed. – RemissibleUTC
for Etc/UTC time zone. Might look like a bug in java.time (I am still using Java 17). – RemissibleDateTimeFormatter
can parse2022-05-24UTC
using patternyyyy-MM-ddz
andUTC12:15
using patternzHH:mm
but not2022-05-24UTC12:15
using patternyyyy-MM-ddzHH:mm
. With non-German locales there isn’t any problem. Consider reporting it. – RemissibleLocale.ENGLISH
, for example… – LinemanDateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-ddzHH:mm", Locale.ROOT);
. – Remissible