日期工具类-汇总当日到年底还剩下多少天

    /**
     * @return 返回当日到当年最后一天一共多少天
     * @param currentDateStr 指定日期
     * @param pattern        日期类型
     * daysOfFeb: 二月天数
     * dayOfMonth:当日
     * j: 当月
     * arrThirtyFirst: 31天数组
     * arrThirty:30天数组
     *  flag:   标志位 用来限定是否第一次访问,如果是第一次计算当月剩余天数,否则累加剩余天数
     */
    public static int remainDays(String currentDateStr) {
        LocalDate now;
        if (StringUtils.isBlank(currentDateStr)) {
            now = LocalDate.now();
        } else {
            now = stringToLocalDate(currentDateStr, com.alibaba.excel.util.DateUtils.DATE_FORMAT_10);
        }
        int year = now.getYear();
        // 平年闰年判断
        boolean yearFlag = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
        int daysOfFeb = (yearFlag) ? 29 : 28;

        int dayOfMonth = now.getDayOfMonth();
        int j = now.getMonthValue();

        int[] arrThirtyFirst = {1, 3, 5, 7, 8, 10, 12};
        List<Integer> handledArrThirtyFirst = Arrays.stream(arrThirtyFirst).boxed().collect(Collectors.toList());
        int[] arrThirty = {4, 6, 9, 11};
        List<Integer> handledArrThirty = Arrays.stream(arrThirty).boxed().collect(Collectors.toList());

        boolean flag = true;
        int total = 0;
        if (j == 2) {
            total += daysOfFeb;
        }
        while (j > 0 && j < 13) {
            if (flag) {
                if (handledArrThirtyFirst.contains(j)) {
                    total += 31 - dayOfMonth;
                }
                if (handledArrThirty.contains(j)) {
                    total += 30 - dayOfMonth;
                }
                flag = false;
            } else {
                if (handledArrThirtyFirst.contains(j)) {
                    total += 31;
                }
                if (handledArrThirty.contains(j)) {
                    total += 30;
                }
            }
            j++;
        }
        return total;
    }

验证

    @Test
    public void testRemainDays() {
        int count = DateUtil.remainDays(null);
        log.info("{}年还剩下{}天", LocalDate.now().getYear(), count);

        int cou = DateUtil.remainDays("2023-11-11");
        log.info("2023年还剩下{}天", cou);

        int first = DateUtil.remainDays("2024-12-01");
        log.info("2024年还剩下{}天", first);

        int second = DateUtil.remainDays("2024-02-01");
        log.info("2024年还剩下{}天", second);

        // 测试用例
        /**
         * 2024.06.12
         * 2024.02.01
         * 2024.12.01
         * 参数为空
         */

    }

14:44:38.864 [main] INFO com.geekmice.springbootselfexercise.date.NoDaoTest - 2024年还剩下202天
14:44:38.882 [main] INFO com.geekmice.springbootselfexercise.date.NoDaoTest - 2023年还剩下50天
14:44:38.882 [main] INFO com.geekmice.springbootselfexercise.date.NoDaoTest - 2024年还剩下30天
14:44:38.883 [main] INFO com.geekmice.springbootselfexercise.date.NoDaoTest - 2024年还剩下335天

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值