英文:
Get a list of Monday dates from Broadcast calender between given range of dates using java
问题
我有一个日期范围从2024年10月7日到2024年12月29日。我需要获取广播日历的所有星期开始日期,即在这个范围内的所有星期一的日期。尝试像下面这样做。我只能获取一个日期的开始日期,希望扩展到获取所有星期一的日期列表。如下所示:
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 dateFormat = new Date("Nov 05, 2024 12:00 AM EST");
BroadcastCalendar broadcastCalendar = new BroadcastCalendar();
LocalDate broadcastStartDate = broadcastCalendar.getWeekStart(Instant.ofEpochMilli(dateFormat.getTime()).atZone(ZoneId.of("UTC")).toLocalDate());
System.out.println("check here "+Date.from(broadcastStartDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
}
英文:
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()));
}
答案1
得分: 3
LocalDate#datesUntil
在Java 9+中,调用LocalDate#datesUntil
方法会提供一个包含LocalDate
对象的Stream
。
如果你知道你的起始日期是星期一,你可以这样做。
var start = LocalDate.of(2024, 10, 7);
var end = LocalDate.of(2024, 12, 29);
var mondays = start.datesUntil(end, Period.ofWeeks(1)).toList();
System.out.println(mondays);
英文:
LocalDate#datesUntil
In Java 9+, calling LocalDate#datesUntil
provides a Stream
of LocalDate
objects.
If you are aware that your start day is a monday you could do the following.
var start = LocalDate.of(2024, 10, 7);
var end = LocalDate.of(2024, 12, 29);
var mondays = start.datesUntil(end, Period.ofWeeks(1)).toList();
System.out.println(mondays);
答案2
得分: 2
如果您使用的是Java 8,您可以简单地循环直到当前日期匹配或超过结束日期。类似这样:
LocalDate startDate = LocalDate.of(2024, 10, 7);
LocalDate endDate = LocalDate.of(2024, 12, 29);
List<LocalDate> mondays = new ArrayList<>();
LocalDate monday = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
while (monday.isBefore(endDate) || monday.isEqual(endDate)) {
mondays.add(monday);
monday = monday.plusDays(7);
}
现在mondays
包含了从2024年10月7日到2024年12月29日(包括在内)的所有星期一日期。
英文:
If you're on Java 8, you could simply loop until your current date matches or exceeds the end date. Something like this:
LocalDate startDate = LocalDate.of(2024, 10, 7);
LocalDate endDate = LocalDate.of(2024, 12, 29);
List<LocalDate> mondays = new ArrayList<>();
LocalDate monday = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
while (monday.isBefore(endDate) || monday.isEqual(endDate)) {
mondays.add(monday);
monday = monday.plusDays(7);
}
Now mondays
contains all Mondays between 2024-10-07 and 2024-12-29 (inclusive).
答案3
得分: 2
使用 Stream.iterate
您可以这样做:
LocalDate start = LocalDate.parse("2024年10月7日", DateTimeFormatter.ofPattern("MMM d, yyyy"));
LocalDate end = LocalDate.parse("2024年12月29日", DateTimeFormatter.ofPattern("MMM d, yyyy"));
Stream
.iterate(
start ,
localDate -> localDate.isBefore(end),
localDate -> localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
)
.forEach(System.out::println);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论