复制代码

为懒人提供无限可能,生命不息,code不止

人类感性的情绪,让我们知难行难
我思故我在
日拱一卒,功不唐捐
  • 首页
  • 前端
  • 后台
  • 数据库
  • 运维
  • 资源下载
  • 实用工具
  • 接口文档工具
  • 登录
  • 注册

其它

【原创】进销存负库存计算持续天数

作者: whooyun发表于: 2023-09-01 16:01

数据特征:

 以下为负库存的时间段,存在一个日期列表
 时间段一
 2023-08-01
 2023-08-02
 
 时间段二
 2023-08-04
 2023-08-05
 
 时间三
 2023-08-09
 2023-08-10
 2023-08-11

负库存持续天数计算

    public   int findLongestConsecutiveDays(List<LocalDate> dates) {
        if (dates == null || dates.isEmpty()) {
            return 0;
        }

        // 对日期列表进行排序
        Collections.sort(dates);

        int longestConsecutiveDays = 1;
        int currentConsecutiveDays = 1;

        // 遍历排序后的日期列表,找出最长连续天数
        for (int i = 1; i < dates.size(); i++) {
            LocalDate previousDate = dates.get(i - 1);
            LocalDate currentDate = dates.get(i);

            // 使用ChronoUnit计算相邻日期之间的天数差异
            long diffInDays = ChronoUnit.DAYS.between(previousDate, currentDate);

            if (diffInDays == 1) {
                currentConsecutiveDays++;
            } else {
                currentConsecutiveDays = 1;
            }

            longestConsecutiveDays = Math.max(longestConsecutiveDays, currentConsecutiveDays);
        }

        return longestConsecutiveDays;
    }