java

import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

class WeekRange {
    private LocalDate startDate;
    private LocalDate endDate;
    private boolean updateFlg;
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

    public WeekRange(LocalDate startDate, LocalDate endDate, boolean updateFlg) {
        this.startDate = startDate;
        this.endDate = endDate;
        this.updateFlg = updateFlg;
    }

    public LocalDate getStartDate() {
        return startDate;
    }

    public LocalDate getEndDate() {
        return endDate;
    }

    public boolean isUpdateFlg() {
        return updateFlg;
    }

    @Override
    public String toString() {
        return "WeekRange{" +
                "startDate=" + startDate.format(formatter) +
                ", endDate=" + endDate.format(formatter) +
                ", updateFlg=" + updateFlg +
                '}';
    }
}

public class WeekRangeCalculator {

    public static List<WeekRange> getWeekRanges(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);

        List<WeekRange> weekRanges = new ArrayList<>();

        // 找到第一个开始日期的周一
        LocalDate currentStart = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        // 找到最后一个结束日期的周日
        LocalDate finalEnd = endDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

        while (!currentStart.isAfter(finalEnd)) {
            LocalDate currentEnd = currentStart.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            boolean updateFlg = isInCurrentWeek(currentStart) || isInCurrentWeek(currentEnd) ||
                                isInLastWeek(currentStart) || isInLastWeek(currentEnd) ||
                                isInNextWeek(currentStart) || isInNextWeek(currentEnd);
            weekRanges.add(new WeekRange(currentStart, currentEnd, updateFlg));

            // 下一个周的开始日期
            currentStart = currentStart.plusWeeks(1);
        }

        return weekRanges;
    }

    private static boolean isInCurrentWeek(LocalDate date) {
        LocalDate today = LocalDate.now();
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return !date.isBefore(startOfWeek) && !date.isAfter(endOfWeek);
    }

    private static boolean isInLastWeek(LocalDate date) {
        LocalDate today = LocalDate.now();
        LocalDate startOfLastWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusWeeks(1);
        LocalDate endOfLastWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).minusWeeks(1);
        return !date.isBefore(startOfLastWeek) && !date.isAfter(endOfLastWeek);
    }

    private static boolean isInNextWeek(LocalDate date) {
        LocalDate today = LocalDate.now();
        LocalDate startOfNextWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).plusWeeks(1);
        LocalDate endOfNextWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).plusWeeks(1);
        return !date.isBefore(startOfNextWeek) && !date.isAfter(endOfNextWeek);
    }

    public static void main(String[] args) {
        String start = "20260801";
        String end = "20260831";
        List<WeekRange> weekRanges = getWeekRanges(start, end);

        for (WeekRange range : weekRanges) {
            System.out.println(range);
        }
    }
}