package com.cfb.oa.m; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; class DateRange { LocalDate start; LocalDate end; public DateRange(LocalDate start, LocalDate end) { this.start = start; this.end = end; } // Getters and setters omitted for brevity } public class DateRangeValidator { public static boolean validateDateRanges(List<DateRange> dateRanges) { if (dateRanges == null || dateRanges.isEmpty()) { return true; // No ranges to validate, consider it valid } DateRange previousRange = null; for (DateRange currentRange : dateRanges) { if (previousRange != null) { // Check for overlap if (!previousRange.end.isBefore(currentRange.start)) { return false; // Overlap detected } // Check for continuity (no gap between ranges) if (!previousRange.end.isEqual(currentRange.start.minusDays(1))) { return false; // Gap detected, not continuous } } previousRange = currentRange; } return true; // No overlaps or gaps detected } public static void main(String[] args) { List<DateRange> dateRanges = new ArrayList<>(); dateRanges.add(new DateRange(LocalDate.of(2023, 10, 1), LocalDate.of(2023, 10, 5))); dateRanges.add(new DateRange(LocalDate.of(2023, 10, 8), LocalDate.of(2023, 10, 10))); // Add more date ranges as needed, ensuring they are continuous and non-overlapping boolean isValid = validateDateRanges(dateRanges); System.out.println("Date ranges are valid: " + isValid); } }
标签:JAVA,DateRange,previousRange,dateRanges,start,从早到晚,end,连贯性,LocalDate From: https://www.cnblogs.com/luodengxiong/p/18678735