I am trying parse '093000' as follows in java:
LocalTime parsed = LocalTime.parse("093000", DateTimeFormatter.ofPattern("hhmmss"));
However I am running into this exception and can't seem to figure it out
Exception in thread "main" java.time.format.DateTimeParseException: Text '093000' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {MinuteOfHour=30, HourOfAmPm=9, SecondOfMinute=0, MicroOfSecond=0, MilliOfSecond=0, NanoOfSecond=0},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
Is it something to do with AM/PM ?
h is the pattern character for the "hour of am/pm" field, not the "hour of day" field, which is likely what you intended.
Since 09 is interpreted as the "hour of am/pm" field, it is ambiguous whether this is 9am or 9pm, so the parsing fails. If you want 09 to mean 9am, and 21 to mean 9pm, you should use the H pattern character.
DateTimeFormatter.ofPattern("HHmmss")
See also the list of pattern characters in the documentation.
Please try HHmmss instead of hhmmss. The hh describes the clock-hour-of-am-pm (1-12) and HH is for hour-of-day (0-23).
LocalTime parsed = LocalTime.parse("093000", DateTimeFormatter.ofPattern("HHmmss"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With