[Java] 取多个时间段内的重叠时间部分 日期类型使用yyyyMMddHHmmss的Long类型(20220513104000)

流程/思路:

package controller;

import java.util.*;
import java.util.stream.Collectors;

public class TestController {

    /**
     * @param unusableTimeList 所有时间段:[startTime, endTime, startTime, endTime, startTime, endTime]
     * @return 返回时间段:[startTime, endTime, startTime, endTime]
     */
    public static List<Long> getOverlapTimeRange(List<Long> unusableTimeList) {
        List<Long> finalUnusableTimeList = new ArrayList<>();
        // 统计时间段,去重
        Map<List<Long>, Integer> timeRangeMap = new HashMap<>();
        // 获取每段时间的时间点,去重
        Set<Long> timeSet = new HashSet<>(unusableTimeList);
        // 将Set转换为List, 并排序
        List<Long> newUnusableTimeList = new ArrayList<>(timeSet).stream().sorted(Long::compareTo).collect(Collectors.toList());
        // 统计每段时间的重复次数
        Map<String, Integer> timeRangeReserCountMap = new HashMap<>();
        for (int i = 0; i < newUnusableTimeList.size(); i++) {
            if (i + 1 == newUnusableTimeList.size()) break;
            // 每段时间的间隔
            Long startInt = newUnusableTimeList.get(i);
            Long endInt = newUnusableTimeList.get(i + 1);
            String key = startInt + "_" + endInt;
            // 每段时间的重复次数
            Integer timeRangeCount = timeRangeReserCountMap.get(key) != null ? timeRangeReserCountMap.get(key) : 0;
            //迭代每段时间范围,算出每子时间段内的重复时间占用数量
            for (int j = 0; j < unusableTimeList.size(); j = j + 2) {
                if (j + 1 == unusableTimeList.size()) break;
                //预约开始结束范围
                Long reserStartInt = unusableTimeList.get(j);
                Long reserEndInt = unusableTimeList.get(j + 1);
                if (reserStartInt <= startInt && endInt <= reserEndInt) {
                    timeRangeCount++;
                }
                if (timeRangeReserCountMap.get(key) != null && timeRangeReserCountMap.get(key) > 1) { // 判断该时间段是否达到某种需求条件,这里为时间段重复次数大于1的就返回
                    timeRangeMap.put(Arrays.asList(startInt, endInt), timeRangeCount);
                }
                timeRangeReserCountMap.put(key, timeRangeCount);
            }
        }
        for (Map.Entry<List<Long>, Integer> entry : timeRangeMap.entrySet()) {
            finalUnusableTimeList.addAll(entry.getKey());
        }
        return finalUnusableTimeList;
    }

    public static void main(String[] args) {
        List<Long> unusableTimeList = new ArrayList<>();
        unusableTimeList.add(20200513000000L);
        unusableTimeList.add(20200513235959L);
        unusableTimeList.add(20200513120000L);
        unusableTimeList.add(20200513235959L);
        unusableTimeList.add(20200513101010L);
        unusableTimeList.add(20200513111111L);
        unusableTimeList.add(20200513131313L);
        unusableTimeList.add(20200513151515L);
        List<Long> overlapTimeRange = getOverlapTimeRange(unusableTimeList);
        System.out.println(overlapTimeRange);

    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joker.our

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值