A LocalDate
day, month and year is needed. Your input has only a month and a year. You will need to select an arbitrary day and install it in the analyzed object in order to create LocalDate
.
You can either parse it on java.time.YearMonth
, and then select the day:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM-yyyy");
YearMonth ym = YearMonth.parse("09-2017", fmt);
LocalDate dt = ym.atDay(1);
Or you can use java.time.format.DateTimeFormatterBuilder
c java.time.temporal.ChronoField
to determine the default value for the day:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("MM-yyyy")
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
.toFormatter();
LocalDate dt = LocalDate.parse("09-2017", fmt);
PS: , YearMonth
( ).
user7605325