解决目的 begin end,去除集合类的其他区间差值List<rang> r1 = new ArrayList(); 得到差值package com.jowoiot.wmzs.utils.date;import com.google.common.collect.Lists; import com.google.common.collect.Range; import org.apache.commons.lang.time.DateFormatUtils;import java.io.FileNotFoundException; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.OptionalInt; import java.util.stream.Collectors; import java.util.stream.IntStream;/*** @BelongsProject: wmzs* @BelongsPackage: com.jowoiot.wmzs.utils.date* @Author: lius* @CreateTime: 2023-08-30 17:36* @Description: 计算一个区间时间,去除其他区间时间保留剩下的差值* @Version: 1.0*/public class TimeRangeUtil {public final static String DATETIME_DEFAULT = "yyyy-MM-dd HH:mm:ss";/*** 获取时间范围差集* 把所有时间按顺序排序,取开始结束时间中间,每一个时间点构成区间,返回不在需要去除的时间区间** @param subtractionDateList 需要去除的时间 [[开始时间,结束时间], [开始时间,结束时间]]* @param min 时间范围开始时间* @param max 时间范围结束时间* @return*/public static List<Date[]> getDateSubtraction(List<Date[]> subtractionDateList, Date min, Date max) {List<Date> dateList = Lists.newArrayList(min, max);for (Date[] dates : subtractionDateList) {dateList.add(dates[0]);dateList.add(dates[1]);}List<Date> sortDateList = dateList.stream().sorted().collect(Collectors.toList());OptionalInt minIndex = IntStream.range(0, sortDateList.size()).filter(i -> sortDateList.get(i).compareTo(min) >= 0).findFirst();OptionalInt maxIndex = IntStream.range(0, sortDateList.size()).filter(i -> sortDateList.get(i).compareTo(max) >= 0).findFirst();List<Date[]> result = new ArrayList<>();if (!minIndex.isPresent() || !maxIndex.isPresent()) {return result;}List<Range<Date>> collect = subtractionDateList.stream().map(dates -> Range.open(dates[0], dates[1])).collect(Collectors.toList());List<Date> subList = sortDateList.subList(minIndex.getAsInt(), maxIndex.getAsInt());subList.add(sortDateList.get(maxIndex.getAsInt()));t:for (int i = 0; i < subList.size(); i++) {if (i == subList.size() - 1 || subList.get(i).compareTo(subList.get(i + 1)) == 0) {continue;}for (Range<Date> dateRange : collect) {if (dateRange.encloses(Range.open(subList.get(i), subList.get(i + 1)))) {continue t;}}result.add(new Date[]{subList.get(i), subList.get(i + 1)});}return result;}public static void main(String[] a) throws FileNotFoundException, ParseException {Date startDate = DateUtils.parseDate("2023-02-08 15:20:00", DATETIME_DEFAULT);Date endDate = DateUtils.parseDate("2023-02-08 16:30:00", DATETIME_DEFAULT);Date a1 = DateUtils.parseDate("2023-02-08 15:00:00", DATETIME_DEFAULT);Date a2 = DateUtils.parseDate("2023-02-08 15:29:00", DATETIME_DEFAULT);Date b1 = DateUtils.parseDate("2023-02-08 15:30:00", DATETIME_DEFAULT);Date b2 = DateUtils.parseDate("2023-02-08 16:00:00", DATETIME_DEFAULT);Date c1 = DateUtils.parseDate("2023-02-08 16:20:00", DATETIME_DEFAULT);Date c2 = DateUtils.parseDate("2023-02-08 16:50:00", DATETIME_DEFAULT);Date d1 = DateUtils.parseDate("2023-02-08 16:40:00", DATETIME_DEFAULT);Date d2 = DateUtils.parseDate("2023-02-08 16:50:00", DATETIME_DEFAULT);System.out.println("开始结束时间:" + DateFormatUtils.format(startDate, DATETIME_DEFAULT) + "—" + DateFormatUtils.format(endDate, DATETIME_DEFAULT));List<Date[]> list = new ArrayList<>();list.add(new Date[]{a1, a2});list.add(new Date[]{b1, b2});list.add(new Date[]{c1, c2});list.add(new Date[]{d1, d2});for (Date[] dates : list) {System.out.println("去除时间:" + DateFormatUtils.format(dates[0], DATETIME_DEFAULT) + "—" + DateFormatUtils.format(dates[1], DATETIME_DEFAULT));}List<Date[]> subtraction = getDateSubtraction(list, startDate, endDate);for (Date[] aa : subtraction) {String s = DateFormatUtils.format(aa[0], DATETIME_DEFAULT);String e = DateFormatUtils.format(aa[1], DATETIME_DEFAULT);System.out.println("差值:" + s + "——" + e);}}}