实现说明
真正干活的是类 PayrollDates和函数 main,其余都是接线。
使用要点
- 直接运行文件,输出会带上标签,比如
month start:、month end:、next friday:——输出的每一行都能指回产生它的那次调用。
常见问题
运行「TemporalAdjusters 业务日期」会打印什么?
文件本身就是完整程序,直接运行——输出行带有 month start:、month end:、next friday: 这类标签。
核心语句
LocalDate today = LocalDate.of(2026, 11, 14);
代码示例
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class PayrollDates {
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 11, 14);
System.out.println("month start: " + today.with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("month end: " + today.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println("next friday: " + today.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
System.out.println("prev or same: " + today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
System.out.println("first monday: " + today.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
System.out.println("year end: " + today.with(TemporalAdjusters.lastDayOfYear()));
}
}