Java program to find the number of days in a month

1. Rules for the number of days in a month in a year

A year has 12 months, each with a different number of days.

The number of days in a month
The number of days in a month

Months with 31 days are 1, 3, 5, 7, 8, 10, 12. Months with 30 days are 4, 6, 9, 11. February has 28 days if it is not a leap year, and 29 days if it is a leap year.

So what is a leap year? A leap year is a year divisible by 4 but not divisible by 100 or divisible by 400.

The program to find the number of days in a month in a year is implemented as follows:

  • Enter the month and year from the keyboard.
  • Check if the month is 1, 3, 5, 7, 8, 10, or 12, then print out that the month has 31 days. If the month is 4, 6, 9, or 11, then print out that the month has 30 days.
  • Check if the month is February, then check if it is a leap year. If it is a leap year, print out that February has 29 days; otherwise, February has 28 days.
  • If the month value is not between 1 and 12, print out that the month is invalid.

2. Using the switch case statement in Java

Use the switch case statement to check the months.

package numberdayinmonth;
import java.util.Scanner;

public class NumberDayInMonth {
    public static boolean checkYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    public static void main(String[] args) {
        int month, year;
        Scanner input = new Scanner(System.in);
        System.out.print("Input month: ");
        month = input.nextInt();
        System.out.print("Input year: ");
        year = input.nextInt();
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.print("Month " + month + "th has 31 days.");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.print("Month " + month + "th has 30 days.");
                break;
            case 2:
                if (checkYear(year)) {
                    System.out.print("Month " + month + "th has 29 days.");
                } else {
                    System.out.print("Month " + month + "th has 28 days.");
                }
                break;
            default:
                System.out.print("Invalid month.");
        }
    }
}

3. Using the if…else statement in Java

Use the if else statement to check the months.

package numberdayinmonth;
import java.util.Scanner;

public class NumberDayInMonth {
    public static boolean checkYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    public static void main(String[] args) {
        int month, year;
        Scanner input = new Scanner(System.in);
        System.out.print("Input month: ");
        month = input.nextInt();
        System.out.print("Input year: ");
        year = input.nextInt();
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            System.out.print("Month " + month + "th has 31 days.");
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            System.out.print("Month " + month + "th has 30 days.");
        } else if (month == 2) {
            if (checkYear(year)) {
                System.out.print("Month " + month + "th has 29 days.");
            } else {
                System.out.print("Month " + month + "th has 28 days.");
            }
        } else {
            System.out.print("Invalid month.");
        }
    }
}

4. A concise way to find the number of days in a month

The above examples use many System.out.print() statements to print the days of the month. As shown below, we can create a variable to store the cases of months with 31 days, 30 days, 29 days, and 28 days. This helps the program to use only one System.out.print() statement to print the number of days in the month.

package numberdayinmonth;
import java.util.Scanner;

public class NumberDayInMonth {
    public static boolean checkYear(int year) {
        return (year% 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    public static void main(String[] args) {
        int month, year;
        // cases of month in year
        int monthcase = 0;
        boolean validMonth = true;
        Scanner input = new Scanner(System.in);
        System.out.print("Input month: ");
        month = input.nextInt();
        System.out.print("Input year: ");
        year = input.nextInt();
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                monthcase = 3;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                monthcase = 2;
                break;
            case 2:
                if (checkYear(year)) {
                    monthcase = 1;
                } else {
                    monthcase = 0;
                }
                break;
            default:
                System.out.print("Invalid month.");
                validMonth = false;
                break;
        }
        if (validMonth) {
            System.out.print("Month " + month + "th has " + (28 + monthcase) + " days.");
        }
    }
}

If the month has 31 days, then monthcase = 3, if the month has 30 days, then monthcase = 2. If it is a leap year and February, then monthcase = 1, otherwise, monthcase = 0. To print the days in the month, we only need to add 28 and monthcase.

5/5 - (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *