I am using YearQuarter from the ThreeTen-Extra library to represent a particular quarter within a given year.
I am using a custom format rather than the default format, e.g. "3Q2018" instead of "2018-Q3" for the third quarter of 2018. When parsing a value through YearQuarter.parse(text, formatter), I want values with leading zeros for the quarter to be rejected (via the standard DateTimeParseException).
My first attempt was to use DateTimeFormatter.ofPattern("Q'Q'yy")
. However, this accepts strings with arbitrarily many leading zeros in front of the quarter. For instance, neither "02Q2007" nor "002Q2007" are rejected by this formatter.
This does seem consistent with how the single character patterns are handled for other temporal types. For instance, these successfully parse without error:
YearMonth.parse("012-2018", DateTimeFormatter.ofPattern("M-yyyy"))
LocalTime.parse("001:00:01", DateTimeFormatter.ofPattern("H:mm:ss"))
LocalTime.parse("01:00:012", DateTimeFormatter.ofPattern("HH:mm:s"))
However, these reject the input with a DateTimeParseException
:
YearMonth.parse("012-2018", DateTimeFormatter.ofPattern("MM-yyyy"))
LocalTime.parse("001:00:01", DateTimeFormatter.ofPattern("HH:mm:ss"))
LocalTime.parse("01:00:012", DateTimeFormatter.ofPattern("HH:mm:ss"))
How can I construct a DateTimeFormatter to parse a YearQuarter
in my desired format while rejecting values with more than one digit for the quarter? Ideally, I'd like to do this as a pattern string, as those are easier for humans to read and are much easier externalize as textual properties.