java 获取当前月每周区间-跨月

114 阅读1分钟

java 获取当前月每周区间-跨月

LocalDate now = LocalDate.now().withDayOfMonth(1);
LocalDate firstDayOfNextMonth = now.with(DayOfWeek.MONDAY);
LocalDate lasDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()).with(DayOfWeek.SUNDAY).plusDays(1);
System.err.println(firstDayOfNextMonth);
System.err.println(lasDayOfMonth);
List<Pair<LocalDate, LocalDate>> list =
        Stream.iterate(new Pair<>(firstDayOfNextMonth, firstDayOfNextMonth.plusDays(7 - firstDayOfNextMonth.getDayOfWeek().getValue()))
                , it -> new Pair<>(it.getValue().plusDays(1), it.getValue().plusDays(7)))
                .limit(8)
                .filter(it -> it.getValue().isBefore(lasDayOfMonth))
                .map(it -> it.getValue().isBefore(lasDayOfMonth) ? it : new Pair<>(it.getValue(), lasDayOfMonth.minusDays(1)))
                .collect(Collectors.toList());
System.err.println(JSON.toJSONString(list));