Java判斷多個時間段是否重合的方法小結
更新時間:2025年02月23日 11:09:06 作者:骨力
這篇文章主要為大家詳細介紹了Java中判斷多個時間段是否重合的方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
判斷多個時間段是否有間隔
實體類內容
public class DateRangeStr { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM", timezone = "GMT+8") private String startDate;//格式:yyyy-MM-dd @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM", timezone = "GMT+8") private String endDate;//格式:yyyy-MM-dd }
主方法內容
//判斷多個時間段是否有時間間隔 true無間隔 false有間隔 public boolean hasNoGaps(List<DateRangeStr> sortedStrRanges) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); if (sortedStrRanges == null || sortedStrRanges.size() <= 0) return false; if (sortedStrRanges.size() == 1) return true; List<DateRange> timePeriods = new ArrayList<>(); for (DateRangeStr ss : sortedStrRanges) { LocalDate startDate = LocalDate.parse(ss.getStartDate(), formatter); LocalDate endDate = LocalDate.parse(ss.getEndDate(), formatter); DateRange dr = new DateRange(); dr.setStartDate(startDate); dr.setEndDate(endDate); timePeriods.add(dr); } Collections.sort(timePeriods, Comparator.comparing(DateRange::getStartDate)); LocalDate previousEndDate = timePeriods.get(0).getEndDate(); for (int i = 1; i < timePeriods.size(); i++) { DateRange currentPeriod = timePeriods.get(i); if (previousEndDate.isAfter(currentPeriod.getStartDate())) { // 當前時間段的開始日期早于上一個時間段的結束日期,存在重疊,這不一定是問題 // 但我們仍然將當前時間段的結束日期作為新的previousEndDate previousEndDate = currentPeriod.getEndDate(); } else if ((!previousEndDate.isEqual(currentPeriod.getStartDate()) && !previousEndDate.plusDays(1L).isEqual(currentPeriod.getStartDate()))) { // 發(fā)現了間隔 return false; } else { // 相鄰時間段之間沒有間隔 previousEndDate = currentPeriod.getEndDate(); } } return true; // 所有時間段都已遍歷完畢,且沒有發(fā)現時間間隔 }
判斷時間段集合是否與某時間段重合
實體類內容
public class TimeInterval { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM", timezone = "GMT+8") private String start; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM", timezone = "GMT+8") private String end; }
主方法內容
private static YearMonth parseYearMonth(String input) { try { return YearMonth.parse(input, MONTH_FORMATTER); } catch (Exception e) { throw new IllegalArgumentException("Invalid date format: " + input, e); } } public static boolean overlapsOrContainedInIntervals(List<TimeInterval> intervals, String startDate,String endDate) { YearMonth yearMonthAStart = parseYearMonth(startDate); YearMonth yearMonthAEnd = parseYearMonth(endDate); for (TimeInterval interval : intervals) { YearMonth start = parseYearMonth(interval.getStart()); YearMonth end = parseYearMonth(interval.getEnd()); // 檢查重疊或包含 if (!start.isAfter(yearMonthAEnd) && !end.isBefore(yearMonthAStart)) { return true; } } return false; }
到此這篇關于Java判斷多個時間段是否重合的方法小結的文章就介紹到這了,更多相關Java判斷時間段是否重合內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java 實戰(zhàn)項目之在線點餐系統(tǒng)的實現流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現一個在線點餐系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11El表達式使用問題javax.el.ELException:Failed to parse the expression
今天小編就為大家分享一篇關于Jsp El表達式使用問題javax.el.ELException:Failed to parse the expression的解決方式,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12