5 ways in Java to print the total number of days in a month

Java program to print the days of a month:

In this post, we will learn how to print the days of a month in Java. The program will take the month number and year as inputs from the user and print the days for that month of the given year.

I will show you different ways to solve this problem.

Algorithm to find the days of the month of a year:

The total number of days is fixed for all the months except February. For February, it will depend on the year. If the year is a leap year, there will be 29 days in February. Else, there will be 28 days.

So, if we find the year is a leap year or not, we can find out the number of days in the given month.

How to find if a year is a leap year or not:

If a year is evenly divisible by 4, it is called a leap year. For century years, it should be evenly divisible by 400. So, we can call a year leap year if:

  • It is evenly divisible by 4.
  • Or, if it is divisible by 100, it is evenly divisible by 400.

Let’s write down the final program.

Example 1: Java program print days of a given month by using nested if-else blocks:

The below Java program takes the month number and year as inputs from the user and print the number of days for that month:

import java.util.Scanner;

class Main {
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int month, year, days = -1;

        System.out.println("Enter the Month number: ");
        month = scanner.nextInt();

        System.out.println("Enter the Year: ");
        year = scanner.nextInt();

        if (month == 1) {
            days = 31;
        } else if (month == 2) {
            days = isLeapYear(year) ? 29 : 28;
        } else if (month == 3) {
            days = 31;
        } else if (month == 4) {
            days = 30;
        } else if (month == 5) {
            days = 31;
        } else if (month == 6) {
            days = 30;
        } else if (month == 7) {
            days = 31;
        } else if (month == 8) {
            days = 31;
        } else if (month == 9) {
            days = 30;
        } else if (month == 10) {
            days = 31;
        } else if (month == 11) {
            days = 30;
        } else if (month == 12) {
            days = 31;
        }

        System.out.println("Number of days: " + days);

    }
}

Here,

  • The isLeapYear method checks if a year is a leap year or not. It takes one year as the parameter and returns one boolean value.
  • It asks the user to enter the month and year values and assigns these to the month and year variables.
  • The if-else blocks checks the value of month and assigns the days value.
  • At the end of the program, it prints the number of days found.

If you run this program, it will print output as below:

Enter the Month number: 
2
Enter the Year: 
1600
Number of days: 29

Example 2: Simplifying the if-else blocks:

We can simplify the nested if-else blocks. We can combine all blocks those assigns 31 and those returns 30 to the days variable.

import java.util.Scanner;

class Main {
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int month, year, days = -1;

        System.out.println("Enter the Month number: ");
        month = scanner.nextInt();

        System.out.println("Enter the Year: ");
        year = scanner.nextInt();

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            days = 31;
        } else if (month == 2) {
            days = isLeapYear(year) ? 29 : 28;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            days = 30;
        }

        System.out.println("Number of days: " + days);

    }
}

It will give similar output.

Example 3: How to print the days of a given month using switch case:

We can use a switch block to write the same program. The switch block takes the month number and it moves to the case based on the value of the month.

import java.util.Scanner;

class Main {
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int month, year, days = -1;

        System.out.println("Enter the Month number: ");
        month = scanner.nextInt();

        System.out.println("Enter the Year: ");
        year = scanner.nextInt();

        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                days = isLeapYear(year) ? 29 : 28;
        }

        System.out.println("Number of days: " + days);

    }
}

Example 4: How to use enhanced switch to print the days of a month:

We can replace the above program with enhanced switch statements. It will make the program clean and readable:

import java.util.Scanner;

class Main {
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int month, year, days = -1;

        System.out.println("Enter the Month number: ");
        month = scanner.nextInt();

        System.out.println("Enter the Year: ");
        year = scanner.nextInt();

        switch (month) {
            case 1, 3, 5, 7, 8, 10, 12 -> days = 31;
            case 4, 6, 9, 11 -> days = 30;
            case 2 -> days = isLeapYear(year) ? 29 : 28;
        }

        System.out.println("Number of days: " + days);

    }
}

Example 5: Print the days of a month by using an array:

We can also use an array to store the total days for each month. For example, the first element of the array will hold the number of days of January, the second element will hold the number of days of February etc.

For February, we will have to check for leap year to decide the total number of days. For other months, we can pick the number directly from the array.

import java.util.Scanner;

class Main {
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] daysArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int month, year, days = -1;

        System.out.println("Enter the Month number: ");
        month = scanner.nextInt();

        System.out.println("Enter the Year: ");
        year = scanner.nextInt();

        if (month == 2 && isLeapYear(year)) {
            days = 29;
        } else {
            days = daysArray[month - 1];
        }

        System.out.println("Number of days: " + days);

    }
}
  • We created an array of integers daysArray to hold the number of days for each month.
  • The if-else block checks if the current month is February and if the year is a leap year or not. If yes, it assigns 29 to the days variable. Else, it picks the value from the daysArray.

It will print similar output.

Enter the Month number: 
8
Enter the Year: 
2022
Number of days: 31

You might also like: