获取日期集合中最大连续月份的个数

 1.获取日期集合中最大连续月份的个数


    /**
     * 获取日期集合中最大连续月份的个数(用于计算最大连续逾期期数)
     *
     * @param dateList
     * @return
     * @throws ParseException
     */
    public int getMaxConsecutiveMonths(List<String> dateList) throws ParseException {
        // 将日期字符串转换为Date对象,并排序日期集合
        List<Date> sortedDates = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (String dateString : dateList) {
            Date date = sdf.parse(dateString);
            sortedDates.add(date);
        }
        Collections.sort(sortedDates);

        // 计算最大连续月份个数
        int maxConsecutiveMonths = 1;
        int currentConsecutiveMonths = 1;

        for (int i = 1; i < sortedDates.size(); i++) {
            // 判断相邻两个日期是否是连续的月份
            long diffInMonths = monthsBetween(sortedDates.get(i - 1), sortedDates.get(i));
            if (diffInMonths == 1) {
                currentConsecutiveMonths++;
                if (currentConsecutiveMonths > maxConsecutiveMonths) {
                    maxConsecutiveMonths = currentConsecutiveMonths;
                }
            } else {
                currentConsecutiveMonths = 1;
            }
        }
        return maxConsecutiveMonths;
    }

 2.获取两个年月之间的所有月份,并进行倒序排序

/**
     * 获取两个年月之间的所有月份,并进行倒序排序
     *
     * @param startDate
     * @param endDate
     * @return
     */
    private static List<String> calculateYearMonths(String startDate, String endDate) {
        List<String> yearMonths = new ArrayList<>();

        YearMonth startYearMonth = YearMonth.parse(startDate);
        YearMonth endYearMonth = YearMonth.parse(endDate);

        YearMonth currentYearMonth = startYearMonth;
        while (!currentYearMonth.isAfter(endYearMonth)) {
            yearMonths.add(currentYearMonth.toString());
            currentYearMonth = currentYearMonth.plusMonths(1);
        }
        Collections.reverse(yearMonths);
        return yearMonths;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值