package com.cfb.oa.m; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; class DateRange { LocalDate startDate; LocalDate endDate; public DateRange(LocalDate startDate, LocalDate endDate) { this.startDate = startDate; this.endDate = endDate; } // Getters for startDate and endDate public LocalDate getStartDate() { return startDate; } public LocalDate getEndDate() { return endDate; } } public class DateRangeUtils { public static LocalDate findMinStartDate(List<DateRange> dateRanges) { if (dateRanges == null || dateRanges.isEmpty()) { throw new IllegalArgumentException("Date range list cannot be null or empty"); } LocalDate minStartDate = LocalDate.MAX; // 初始化为LocalDate的最大可能值 for (DateRange dateRange : dateRanges) { LocalDate startDate = dateRange.getStartDate(); if (startDate.isBefore(minStartDate)) { minStartDate = startDate; } } return minStartDate; } public static LocalDate findMaxEndDate(List<DateRange> dateRanges) { if (dateRanges == null || dateRanges.isEmpty()) { throw new IllegalArgumentException("Date range list cannot be null or empty"); } LocalDate maxEndDate = LocalDate.MIN; // 初始化为LocalDate的最小可能值 for (DateRange dateRange : dateRanges) { LocalDate endDate = dateRange.getEndDate(); if (endDate.isAfter(maxEndDate)) { maxEndDate = endDate; } } return maxEndDate; } public static void main(String[] args) { List<DateRange> dateRanges = new ArrayList<>(); dateRanges.add(new DateRange(LocalDate.of(2023, 1, 15), LocalDate.of(2023, 1, 20))); dateRanges.add(new DateRange(LocalDate.of(2022, 12, 10), LocalDate.of(2022, 12, 15))); dateRanges.add(new DateRange(LocalDate.of(2023, 3, 5), LocalDate.of(2023, 3, 10))); LocalDate minStartDate = findMinStartDate(dateRanges); LocalDate maxEndDate = findMaxEndDate(dateRanges); System.out.println("Minimum start date: " + minStartDate); System.out.println("Maximum end date: " + maxEndDate); } }
标签:startDate,java,DateRange,dateRanges,日期,数组,endDate,LocalDate,public From: https://www.cnblogs.com/luodengxiong/p/18678770