My parser may encounter \"2:37PM\" (parsed by \"H:mma\") or \"02:37PM\" (parsed by \"hh:mma\"). How can I parse both without resorting to a try-catch?
I receive an erro
You did say you wanted to parse the string. So you can do the following.
for (String s : new String[] { "02:37PM", "2:37PM" }) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("h:mma"); LocalTime lt = LocalTime.parse(s, dtf); System.out.println(lt.format(dtf)); }
Prints
2:37PM 2:37PM