import java.time.DayOfWeek; import java.time.LocalDate; import java.time.YearMonth; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; 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()); List<LocalDate> list = new ArrayList<>(); for (LocalDate date = firstDateOfTheMonth; !date .isAfter(firstDateOfTheMonth.with(TemporalAdjusters.lastDayOfMonth())); date = date.plusDays(1)) if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) list.add(date); return list; } /* * 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); List<LocalDate> list = new ArrayList<>(); for (LocalDate date = firstDateOfTheMonth; !date .isAfter(firstDateOfTheMonth.with(TemporalAdjusters.lastDayOfMonth())); date = date.plusDays(1)) if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) list.add(date); return list; } }
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]