import java.time.DayOfWeek; import java.time.LocalDate; import java.time.YearMonth; import java.time.temporal.TemporalAdjusters; import java.util.List; import java.util.stream.Collectors; public class Main { // Test } /* * All weekends (Sat & Sun) of the given month in the current year */ static List<LocalDate> getWeekends(int month) { LocalDate firstDateOfTheMonth = LocalDate.now().withMonth(month).with(TemporalAdjusters.firstDayOfMonth()); return firstDateOfTheMonth .datesUntil(firstDateOfTheMonth.plusMonths(1)) .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) .collect(Collectors.toList()); } /* * All weekends (Sat & Sun) of the given year and the month */ static List<LocalDate> getWeekends(int year, int month) { LocalDate firstDateOfTheMonth = YearMonth.of(year, month).atDay(1); return firstDateOfTheMonth .datesUntil(firstDateOfTheMonth.plusMonths(1)) .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) .collect(Collectors.toList()); } }
Standard input is empty
[2021-02-06, 2021-02-07, 2021-02-13, 2021-02-14, 2021-02-20, 2021-02-21, 2021-02-27, 2021-02-28] [2020-02-01, 2020-02-02, 2020-02-08, 2020-02-09, 2020-02-15, 2020-02-16, 2020-02-22, 2020-02-23, 2020-02-29]