一、查询本周、本月、本年、的开始日期和结束日期(数据统计时有时候会用到)
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate endOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
LocalDate startOfMonth = now.withDayOfMonth(1);
LocalDate endOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());
LocalDate startOfYear = now.withDayOfYear(1);
LocalDate endOfYear = now.withDayOfYear(365); // 或者使用 TemporalAdjusters.lastDayOfYear()
System.out.println("本周开始日期: " + startOfWeek);
System.out.println("本周结束日期: " + endOfWeek);
System.out.println("本月开始日期: " + startOfMonth);
System.out.println("本月结束日期: " + endOfMonth);
System.out.println("本年开始日期: " + startOfYear);
System.out.println("本年结束日期: " + endOfYear);
}
标签:System,本月,查询,日期,println,now,LocalDate,out
From: https://www.cnblogs.com/xiaoguo-java/p/18315624