I am using an application written in Java, that uses LocalDate.parse with a DateTimeFormatter:
LocalDate.parse("Wed Jun 03 12:00:00 PM GMT 2020", DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss a zzz yyyy"));
I can only provide the format string (I cannot modify the source code). I need it to successfully parse a somewhat crazy date format that looks like this:
Wed Jun 03 12:00:00 PM GMT 2020
The closest format I've gotten to is:
EEE MMM dd hh:mm:ss a zzz yyyy
but this bombs out with
Exception java.time.format.DateTimeParseException: Text 'Wed Jun 03 12:00:00 PM GMT 2020' could not be parsed at index 20
If I remove the AM/PM in the date and format, all works fine. Any tweaks that will make this parse successfully?
tl;dr
ZonedDateTime
, notLocalDate
LocalDate
is the wrong class for your input. ALocalDate
has only a date, no time-of-day, and no time zone.You should be using
ZonedDateTime
class.Specify locale for localization rules
Define a custom format matching your output using the
DateTimeFormatter
class.Specify a
Locale
. Your input is localized in multiple ways: Name of day of week, name of month, and AM/PM. Choose aLocale
that represents the localization choices seen in your inputs. I believeLocale.US
might work.LocalDate
If you really want only the date portion, and wish to discard the time-of-day and offset/zone, then parse as a
LocalDate
while using the same correct formatting pattern.See this code run at Ideone.com. Be aware that the date extracted is the date as seen in the parsed time zone.
ISO 8601
The ideal solution is educate the publisher of your data about the ISO 8601 standard for communicating date-time values unambiguously as text.
Your example input should have been sent as: 2020-06-03T12:00Z
The
Z
on the end is an abbreviation of+00:00
, pronounced “Zulu”, and means an offset of zero hours-minutes-seconds ahead of the temporal meridian of UTC.