I am trying to parse strings in the format of "<year> <quarter>" ("2022 1", "2022 2") into java.time.LocalDate objects. The closest related question I could find is this, but there the author also has the month. If I try to use
DateTimeFormatter quarterFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy q")
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1).toFormatter();
LocalDate.parse("2022 2", quarterFormatter);
I get the error java.time.format.DateTimeParseException: (...) Unable to obtain LocalDate from TemporalAccessor: {Year=2022, QuarterOfYear=2, DayOfMonth=1}
, which doesn't really make sense to me, since that is all the data required to figure out a calendar date.
What am I doing wrong?
Also, as a related question, the original format is actually without a whitespace between the year and the quarter, but if I try to run
DateTimeFormatter quarterFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyyq")
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1).toFormatter();
LocalDate.parse("20222", quarterFormatter);
I get java.time.format.DateTimeParseException: Text '20222' could not be parsed at index 0
. That is also confusing, since parsing from other formats seems to work even without separators.