Java 多组时间取交集、并集、合并

1.1现在我们有一组数据

[{'begintime':2020-01-01 00:04:00,'endtime':2020-01-01 00:06:00},
{'begintime':2020-01-01 00:01:05,'endtime':2020-01-01 00:03:15},
{'begintime':2020-01-01 00:09:00,'endtime':2020-01-01 00:10:00},
{'begintime':2020-01-01 00:07:35,'endtime':2020-01-01 00:08:40},
{'begintime':2020-01-01 00:12:00,'endtime':2020-01-01 00:17:00},
{'begintime':2020-01-01 00:12:05,'endtime':2020-01-01 00:13:10},
{'begintime':2020-01-01 00:19:00,'endtime':2020-01-01 00:21:00},
{'begintime':2020-01-01 00:15:20,'endtime':2020-01-01 00:20:45}]

1.2希望处理完后输出结果

[{'begintime':2020-01-01 00:01:05,'endtime':2020-01-01 00:03:15},
{'begintime':2020-01-01 00:04:00,'endtime':2020-01-01 00:06:00},
{'begintime':2020-01-01 00:07:35,'endtime':2020-01-01 00:08:40},
{'begintime':2020-01-01 00:09:00,'endtime':2020-01-01 00:10:00},
{'begintime':2020-01-01 00:12:00,'endtime':2020-01-01 00:21:00}]

 1.3废话不多说直接上代码
 

public static void main(String[] args) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-dd-mm HH:mm:ss");
        // 先转成List
        String json = "" +
               "[{'begintime':'2020-01-01 00:04:00','endtime':'2020-01-01 00:06:00'},\n" +
                "{'begintime':'2020-01-01 00:01:05','endtime':'2020-01-01 00:03:15'},\n" +
                "{'begintime':'2020-01-01 00:09:00','endtime':'2020-01-01 00:10:00'},\n" +
                "{'begintime':'2020-01-01 00:07:35','endtime':'2020-01-01 00:08:40'},\n" +
                "{'begintime':'2020-01-01 00:12:00','endtime':'2020-01-01 00:17:00'},\n" +
                "{'begintime':'2020-01-01 00:12:05','endtime':'2020-01-01 00:13:10'},\n" +
                "{'begintime':'2020-01-01 00:19:00','endtime':'2020-01-01 00:21:00'},\n" +
                "{'begintime':'2020-01-01 00:15:20','endtime':'2020-01-01 00:20:45'}]";
        // 将JSON转称List
        List<TimeEntity> timeEntities = JSON.parseArray(json, TimeEntity.class);
        // 将List转map
        Map<String, String> listToMap = timeEntities.stream().collect(Collectors.toMap(TimeEntity::getBegintime, TimeEntity::getEndtime));
        // 使用TreeMap将时间排序好
        TreeMap<String, String> dateTreeMap = new TreeMap<>(listToMap);
        // 将时间合并起来
        Iterator<Map.Entry<String, String>> iterator = dateTreeMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            Date beginDate = simpleDateFormat.parse(next.getKey());
            Date endDate = simpleDateFormat.parse(next.getValue());
            mergeDate(beginDate, endDate);
        }
        // 遍历输出时间
        Iterator<Map.Entry<Date, Date>> skipMap = skipListMap.entrySet().iterator();
        while (skipMap.hasNext()) {
            Map.Entry<Date, Date> next = skipMap.next();
            System.out.println("开始时间:" + simpleDateFormat.format(next.getKey()) + "---结束时间:" + simpleDateFormat.format(next.getValue()));
        }
    }

    // 合并时间
    public static void mergeDate(Date beginDateB, Date endDateB) {
        // 如果开始时间在结束时间之后,直接返回
        if (beginDateB.after(endDateB)) return;
        // 先将第一个时间添加到Map里
        if (CollectionUtils.isEmpty(skipListMap)) {
            skipListMap.put(beginDateB, endDateB);
            return;
        }
        // 遍历进行时间比较
        Iterator<Map.Entry<Date, Date>> iterator = skipListMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Date, Date> next = iterator.next();
            Date beginDateA = next.getKey();
            Date endDateA = next.getValue();
            if (beginDateB.before(beginDateA) && (endDateB.after(beginDateA) || endDateB.equals(beginDateA)) && (endDateB.equals(endDateA) || endDateB.before(endDateA))) {
                skipListMap.put(beginDateB, endDateA);
            } else if (beginDateB.before(beginDateA) && endDateB.after(endDateA)) {
                skipListMap.put(beginDateB, endDateB);
            } else if (beginDateB.after(beginDateA) && (endDateB.after(endDateA) || endDateB.equals(endDateA)) && (beginDateB.equals(beginDateA) || beginDateB.before(endDateA))) {
                skipListMap.put(beginDateA, endDateB);
            } else if (endDateA.before(beginDateB) || beginDateA.after(endDateB)) {
                if (!iterator.hasNext()) skipListMap.put(beginDateB, endDateB);
            }
        }
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值