I’m trying to make the hours 1 or two digits and optional by modifying the ISO_LOCAL_TIME formatter. This is more for parsing durations where hours can be optional. This is what I tried.
new DateTimeFormatterBuilder()
.optionalStart()
.appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE)
.appendLiteral(':')
.optionalEnd()
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 0, 9, true)
.toFormatter()
.withResolverStyle(STRICT);
This is what I am testing with.
12:34:56.789
2:34:56.789
34:56.789
34:56 (mm:ss not hh:mm)
But it fails on the last two.
How can I create a formatter which passes all 4 tests?