1. LocalDate date = LocalDate.of(2000, Month.OCTOBER, 15);
  2. DayOfWeek dotw = date.getDayOfWeek();
  3. System.out.printf("%s is on a %s%n", date, dotw);//这一天是周几?
  4. System.out.printf("first day of Month: %s%n", date.with(TemporalAdjusters.firstDayOfMonth()));
  5. System.out.printf("first Monday of Month: %s%n", date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
  6. System.out.printf("last day of Month: %s%n", date.with(TemporalAdjusters.lastDayOfMonth()));
  7. System.out.printf("first day of next Month: %s%n", date.with(TemporalAdjusters.firstDayOfNextMonth()));
  8. System.out.printf("first day of next Year: %s%n", date.with(TemporalAdjusters.firstDayOfNextYear()));
  9. System.out.printf("first day of Year: %s%n", date.with(TemporalAdjusters.firstDayOfYear()));

自定义调节器

  1. /**
  2. * The adjustInto method accepts a Temporal instance
  3. * and returns an adjusted LocalDate. If the passed in
  4. * parameter is not a LocalDate, then a DateTimeException is thrown.
  5. */
  6. public Temporal adjustInto(Temporal input) {
  7. LocalDate date = LocalDate.from(input);
  8. int day;
  9. if (date.getDayOfMonth() < 15) {
  10. day = 15;
  11. } else {
  12. day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
  13. }
  14. date = date.withDayOfMonth(day);
  15. if (date.getDayOfWeek() == DayOfWeek.SATURDAY ||
  16. date.getDayOfWeek() == DayOfWeek.SUNDAY) {
  17. date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
  18. }
  19. return input.with(date);
  20. }