Java当前日期-过去30天-未来30天等常用,以及本周对应日期案例

        System.out.println("---------------------");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        DateTimeFormatter nowFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String nowDate = currentDate.format(nowFormatter);
        System.out.println("当前:"+nowDate);

        // 加上30天
        LocalDate futureDate = currentDate.plusDays(30);

        // 按照指定格式输出日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate1 = futureDate.format(formatter);

        System.out.println("30天后:"+formattedDate1);

        // 减去30天
        LocalDate pastDate = currentDate.minusDays(30);

        // 按照指定格式输出日期
        String formattedDate2 = pastDate.format(formatter);

        System.out.println("30天前:"+formattedDate2);
        System.out.println("---------------------");
        System.out.println("---------------------");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
                // 如果当前日期是周五
        if (currentDate.getDayOfWeek() == DayOfWeek.FRIDAY) {
            LocalDate monday = currentDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
            LocalDate tuesday = monday.plusDays(1);
            LocalDate wednesday = tuesday.plusDays(1);
            LocalDate thursday = wednesday.plusDays(1);

            // 按照指定格式输出日期
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            System.out.println("本周一:" + monday.format(timeFormatter));
            System.out.println("本周二:" + tuesday.format(timeFormatter));
            System.out.println("本周三:" + wednesday.format(timeFormatter));
            System.out.println("本周四:" + thursday.format(timeFormatter));
            System.out.println("本周五:" + currentDate.format(timeFormatter));
        }
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 如果当前日期不是周五,则将日期调整为本周五
        if (currentDate.getDayOfWeek() != DayOfWeek.FRIDAY) {
            LocalDate friday = currentDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
            currentDate = friday;
        }

        LocalDate monday = currentDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
        LocalDate tuesday = monday.plusDays(1);
        LocalDate wednesday = tuesday.plusDays(1);
        LocalDate thursday = wednesday.plusDays(1);

        // 按照指定格式输出日期
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        System.out.println("本周一:" + monday.format(formatter3));
        System.out.println("本周二:" + tuesday.format(formatter3));
        System.out.println("本周三:" + wednesday.format(formatter3));
        System.out.println("本周四:" + thursday.format(formatter3));
        System.out.println("本周五:" + currentDate.format(formatter3));
---------------------
当前:2024-01-17
30天后:2024-02-16
30天前:2023-12-18
---------------------
本周一:2024-01-15
本周二:2024-01-16
本周三:2024-01-17
本周四:2024-01-18
本周五:2024-01-19

你可能感兴趣的:(Java及框架,java,开发语言)