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
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值