我试图得到一个月的第一个星期日。我达到的是获得一年中的所有星期日
import static java.time.temporal.TemporalAdjusters.firstInMonth;
public static void FindAllSundaysOfTheYear () {
// Create a LocalDate object that represent the first day of the year.
int year = 2021;
LocalDate now = LocalDate.of(year, Month.JANUARY, 1);
// Find the first Sunday of the year
LocalDate sunday = now.with(firstInMonth(DayOfWeek.SUNDAY));
do {
// Loop to get every Sunday by adding Period.ofDays(7) the the current Sunday.
System.out.println(sunday.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));
sunday = sunday.plus(Period.ofDays(7));
} while (sunday.getYear() == year);
}
Run Code Online (Sandbox Code Playgroud)
输出是
Sunday, January 5, 2020
Sunday, January 12, 2020
Sunday, …Run Code Online (Sandbox Code Playgroud)java ×1