Используйте DateTimeFormatterBuilder для разбора дат пропущенного дня и по умолчанию до конца месяца - PullRequest
0 голосов
/ 17 июня 2019

Мне нужно разобрать даты в разных форматах.Некоторым из них не хватает «дня».Теперь я хочу по умолчанию до конца месяца.Я не уверен, есть ли прямой путь, по которому мы можем по умолчанию установить конец месяца) и поэтому мне нужно настроить его вручную, используя LocalDate.parse("11.2017", f).with(TemporalAdjusters.lastDayOfMonth()).

1 Ответ

1 голос
/ 17 июня 2019
DateTimeFormatter f = new DateTimeFormatterBuilder()
      .appendPattern("MM.yyyy")
      .parseDefaulting(DAY_OF_MONTH, 31) // set 31
      .toFormatter();

Просто установите DAY_OF_MONTH с 31, DAY_OF_MONTH будет основываться на rangeUnit, чтобы выбрать последний день из месяц анализа .

/**
 * The day-of-month.
 * <p>
 * This represents the concept of the day within the month.
 * In the default ISO calendar system, this has values from 1 to 31 in most months.
 * April, June, September, November have days from 1 to 30, while February has days
 * from 1 to 28, or 29 in a leap year.
 * <p>
 * Non-ISO calendar systems should implement this field using the most recognized
 * day-of-month values for users of the calendar system.
 * Normally, this is a count of days from 1 to the length of the month.
 */
DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),

Пример:

LocalDate parse = LocalDate.parse("02.2017", f);
System.out.println(parse.getDayOfMonth());

Вывод:

28

...