查询本周、本月、本年、的开始日期和结束日期

一、查询本周、本月、本年、的开始日期和结束日期(数据统计时有时候会用到)

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);
        }
posted @ 2024-07-22 10:53  青喺半掩眉砂  阅读(29)  评论(0编辑  收藏  举报