Java实现关于日期连续显示问题

关于日期连续显示问题

	public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.now();
        // 有序list
        List<LocalDate> dates = new ArrayList<>(45);
        dates.add(LocalDate.now().minusYears(1).minusDays(6));
        dates.add(LocalDate.now().minusYears(1).minusDays(5));
        dates.add(LocalDate.now().minusYears(1).minusDays(2));
        dates.add(LocalDate.now().minusYears(1).minusDays(1));
        dates.add(LocalDate.now().minusYears(1));

        LocalDate localDate = LocalDate.now().minusMonths(3);
        for (int i = 0; i < 20; i++) {
            dates.add(localDate.plusDays(i));
        }

        localDate = LocalDate.now().minusMonths(1);
        for (int i = 0; i < 20; i++) {
            dates.add(localDate.plusDays(i));
        }

        System.out.println("============处理之前============");
        for (LocalDate date : dates) {
            System.out.println(date);
        }

        List<String> list = getAcronymDateForContinuity(dates);
        System.out.println("============处理之后============");
        for (String str : list) {
            System.out.println(str);
        }
        System.out.println("总耗时:" + (LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() - startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()) + "ms");

        System.out.println("============还原============");
        List<LocalDate> localDates = getContinuousDate(list);
        for (LocalDate date : localDates) {
            System.out.println(date);
        }
    }

    /**
     * 获取连续日期
     *
     * @param dates 递增日期集合
     * @return
     */
    private static List<LocalDate> getContinuousDate(List<String> dates) {
        List<LocalDate> localDates = new LinkedList<>();
        LocalDate beginDate;
        LocalDate endDate;
        for (String str : dates) {
            String[] strArr = str.split("\\~");
            if (strArr.length > 1) {
                beginDate = LocalDate.parse(strArr[0].trim(), DateTimeFormatter.ISO_DATE);
                // 添加开始日期
                localDates.add(beginDate);
                endDate = LocalDate.parse(strArr[strArr.length - 1].trim(), DateTimeFormatter.ISO_DATE);
                // 添加省略部分日期
                while (true) {
                    beginDate = beginDate.plusDays(1);
                    if (!beginDate.equals(endDate)) {
                        localDates.add(beginDate);
                    } else {
                        break;
                    }
                }
                // 添加结束日期
                localDates.add(endDate);
            } else {
                beginDate = LocalDate.parse(strArr[0].trim(), DateTimeFormatter.ISO_DATE);
                localDates.add(beginDate);
            }
        }
        return localDates;
    }

    /**
     * 获取连续日期的缩略形式
     *
     * @param dates
     * @return
     */
    private static List<String> getAcronymDateForContinuity(List<LocalDate> dates) {
        List<String> dateStrList = new ArrayList<>(dates.size());
        int result;
        for (int i = 0; i < dates.size(); i++) {
            result = getLastIndexForContinuousDate(i, dates);
            if (result == i) {
                dateStrList.add(dates.get(i).toString());
            } else if (result == i + 1) {
                dateStrList.add(dates.get(i).toString());
                dateStrList.add(dates.get(result).toString());
                i = result;
            } else if (result > i + 1) {
                dateStrList.add(dates.get(i) + " ~ " + dates.get(result));
                i = result;
            }
        }
        return dateStrList;
    }

    /**
     * 获取从begin开始的连续日期的最后一位下标
     *
     * @param begin 开始查找下标
     * @param dates 递增日期集合
     * @return
     */
    private static int getLastIndexForContinuousDate(int begin, List<LocalDate> dates) {
        if (begin < dates.size() - 1) {
            if (dates.get(begin).plusDays(1).equals(dates.get(++begin))) {
                if (begin < dates.size() - 1) {
                    return getLastIndexForContinuousDate(begin, dates);
                }
                return begin;
            }
        }
        return begin - 1;
    }

测试输出

============处理之前============
2020-10-15
2020-10-16
2020-10-19
2020-10-20
2020-10-21
2021-07-21
2021-07-22
2021-07-23
2021-07-24
2021-07-25
2021-07-26
2021-07-27
2021-07-28
2021-07-29
2021-07-30
2021-07-31
2021-08-01
2021-08-02
2021-08-03
2021-08-04
2021-08-05
2021-08-06
2021-08-07
2021-08-08
2021-08-09
2021-09-21
2021-09-22
2021-09-23
2021-09-24
2021-09-25
2021-09-26
2021-09-27
2021-09-28
2021-09-29
2021-09-30
2021-10-01
2021-10-02
2021-10-03
2021-10-04
2021-10-05
2021-10-06
2021-10-07
2021-10-08
2021-10-09
2021-10-10
============处理之后============
2020-10-15
2020-10-16
2020-10-19 ~ 2020-10-21
2021-07-21 ~ 2021-08-09
2021-09-21 ~ 2021-10-10
总耗时:2ms
============还原============
2020-10-15
2020-10-16
2020-10-19
2020-10-20
2020-10-21
2021-07-21
2021-07-22
2021-07-23
2021-07-24
2021-07-25
2021-07-26
2021-07-27
2021-07-28
2021-07-29
2021-07-30
2021-07-31
2021-08-01
2021-08-02
2021-08-03
2021-08-04
2021-08-05
2021-08-06
2021-08-07
2021-08-08
2021-08-09
2021-09-21
2021-09-22
2021-09-23
2021-09-24
2021-09-25
2021-09-26
2021-09-27
2021-09-28
2021-09-29
2021-09-30
2021-10-01
2021-10-02
2021-10-03
2021-10-04
2021-10-05
2021-10-06
2021-10-07
2021-10-08
2021-10-09
2021-10-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值