I have a date range Oct 7, 2024 to Dec 29, 2024. I need to fetch all the weeks start date of a broadcast calendar that is all Monday dates between this range. Trying to do something like below. I'm only able to get start of one date would like to extend this to fetch me list of all Monday dates. Oct 7 Oct 14 Oct 21 Oct 28 Nov 4 Nov 11 Nov 18 Nov 25 Dec 02 Dec 09 Dec 16 Dec 23
@Test
public void toBroadcastStartDate() {
Date dateFormate = new Date("Nov 05, 2024 12:00 AM EST");
BroadcastCalendar broadcastCalendar = new BroadcastCalendar();
LocalDate broadcastStartDate = broadcastCalendar.getWeekStart(Instant.ofEpochMilli(dateFormate.getTime()).atZone(ZoneId.of("UTC")).toLocalDate());
System.out.println("check here "+Date.from(broadcastStartDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
}
Using Stream.iterate
you could do this:
LocalDate start = LocalDate.parse("Oct 7, 2024", DateTimeFormatter.ofPattern("MMM d, yyyy"));
LocalDate end = LocalDate.parse("Dec 29, 2024", DateTimeFormatter.ofPattern("MMM d, yyyy"));
Stream
.iterate(
start ,
localDate -> localDate.isBefore(end),
localDate -> localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
)
.forEach(System.out::println);