提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建自定义日期调整器
import static java.time.DayOfWeek.FRIDAY; import static java.time.DayOfWeek.MONDAY; import static java.time.DayOfWeek.SATURDAY;/** 来 自 时 代 J a v a - nowjava.com**/ import static java.time.DayOfWeek.SUNDAY; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAdjusters; /*来自 时代Java公众号 - nowjava.com*/ public class Main { public static void main(String[] args) { LocalDate ld1 = LocalDate.of(2013, Month.DECEMBER, 12); // Thursday LocalDate ld2 = LocalDate.of(2013, Month.DECEMBER, 13); // Friday LocalDate ld3 = LocalDate.of(2013, Month.DECEMBER, 14); // Saturday LocalDate ld1Adjusted = ld1.with(WEEKDAYS_WITH_NO_FRIDAY_13); System.out.println(ld1 + " adjusted to " + ld1Adjusted); LocalDate ld2Adjusted = ld2.with(WEEKDAYS_WITH_NO_FRIDAY_13); System.out.println(ld2 + " adjusted to " + ld2Adjusted); LocalDate ld3Adjusted = ld3.with(WEEKDAYS_WITH_NO_FRIDAY_13); System.out.println(ld3 + " adjusted to " + ld3Adjusted); // Use it to adjust a ZonedDateTime ZonedDateTime zdt = ZonedDateTime.of(ld2, LocalTime.of(8, 45), ZoneId.of("America/Chicago")); ZonedDateTime zdtAdjusted = zdt.with(WEEKDAYS_WITH_NO_FRIDAY_13); System.out.println(zdt + " adjusted to " + zdtAdjusted); } static TemporalAdjuster WEEKDAYS_WITH_NO_FRIDAY_13 = TemporalAdjusters .ofDateAdjuster(Main::getWeekDayNoFriday13); private static LocalDate getWeekDayNoFriday13(LocalDate da