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);
}
}
}