How to print every month in a specified year that contains five full weekends (Fri, Sat, Sun) in Java

1 Answer

0 votes
// A month has five full weekends when it has 31 days, and the 1st day of the month is a Friday.

import java.time.DayOfWeek;
import java.time.LocalDate;

public class FiveFullWeekends {

    // ------------------------------------------------------------
    // Month names as a single reusable constant
    // ------------------------------------------------------------
    private static final String[] monthNames = {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    };

    // ------------------------------------------------------------
    // Function: returns true if a month has 5 full weekends
    // ------------------------------------------------------------
    static boolean hasFiveFullWeekends(int year, int month) {
        LocalDate firstDay = LocalDate.of(year, month, 1);

        int daysInMonth = firstDay.lengthOfMonth();
        DayOfWeek wd = firstDay.getDayOfWeek();

        return (daysInMonth == 31 && wd == DayOfWeek.FRIDAY);
    }

    // ------------------------------------------------------------
    // Main program
    // ------------------------------------------------------------
    public static void main(String[] args) {
        int y_value = 2026;

        for (int m = 1; m <= 12; m++) {
            if (hasFiveFullWeekends(y_value, m)) {
                System.out.println(monthNames[m - 1] + " " + y_value
                        + " has five full weekends.");
            }
        }
    }
}



/*
run:

May 2026 has five full weekends.

*/

 



answered May 26 by avibootz
edited May 26 by avibootz

Related questions

...