83. Getting the first and last day of the week
Let’s assume given an integer (nrOfWeeks
) representing the number of weeks that we want to extract the first and last day of each week starting from now. For instance, for the given nrOfWeeks
= 3 and a local date such as 06/02/2023, we want this:
[
Mon 06/02/2023,
Sun 12/02/2023,
Mon 13/02/2023,
Sun 19/02/2023,
Mon 20/02/2023,
Sun 26/02/2023
]
This is much easier than it might seem. We just need a loop from 0 to nrOfWeeks
and two TemporalAdjusters
to fit the first/last day of each week. More precisely, we need the nextOrSame(DayOfWeek dayOfWeek)
and previousOrSame(DayOfWeek dayOfWeek)
adjusters.
The nextOrSame()
adjuster’s role is to adjust the current date to the first occurrence of the given day of week after the date being adjusted (this can be next or same). On the other hand, the previousOrSame()
adjuster’s role is to adjust the current date to the first occurrence of the given day of week before...