How can I parse a time duration with leap second to seconds?
Example, we are receiving from another service the following duration 00:00:60 which means 1 minute but Java 8 DateTimeFormatter.ofPattern("HH:mm:ss[.SSS]")
throws
java.time.format.DateTimeParseException: Text '00:00:60' could not be parsed: Invalid value for SecondOfMinute (valid values 0 - 59): 60
Which makes sense because there is no notion of 60 seconds in the ISO standard used by Java.
I would like to avoid using String::replace
(as suggested in other SO answers to similar case) and the use of external library if possible.
This is the current code that is throwing the exception:
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss[.SSS]");
public static Integer parseToSeconds(String durationText) {
return LocalTime.parse(durationText, TIME_FORMATTER).toSecondOfDay();
}