w3resource

Java: Find the number of days in a month


Days in a Month

Write a Java program to find the number of days in a month.

Test Data
Input a month number: 2
Input a year: 2016

Pictorial Presentation:

Java conditional statement Exercises: Find the number of days in a month


Sample Solution:

Java Code:

import java.util.Scanner;
public class Exercise7 {

    
  public static void main(String[] strings) {

        Scanner input = new Scanner(System.in);

        int number_Of_DaysInMonth = 0; 
        String MonthOfName = "Unknown";

        System.out.print("Input a month number: ");
        int month = input.nextInt();

        System.out.print("Input a year: ");
        int year = input.nextInt();

        switch (month) {
            case 1:
                MonthOfName = "January";
                number_Of_DaysInMonth = 31;
                break;
            case 2:
                MonthOfName = "February";
                if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
                    number_Of_DaysInMonth = 29;
                } else {
                    number_Of_DaysInMonth = 28;
                }
                break;
            case 3:
                MonthOfName = "March";
                number_Of_DaysInMonth = 31;
                break;
            case 4:
                MonthOfName = "April";
                number_Of_DaysInMonth = 30;
                break;
            case 5:
                MonthOfName = "May";
                number_Of_DaysInMonth = 31;
                break;
            case 6:
                MonthOfName = "June";
                number_Of_DaysInMonth = 30;
                break;
            case 7:
                MonthOfName = "July";
                number_Of_DaysInMonth = 31;
                break;
            case 8:
                MonthOfName = "August";
                number_Of_DaysInMonth = 31;
                break;
            case 9:
                MonthOfName = "September";
                number_Of_DaysInMonth = 30;
                break;
            case 10:
                MonthOfName = "October";
                number_Of_DaysInMonth = 31;
                break;
            case 11:
                MonthOfName = "November";
                number_Of_DaysInMonth = 30;
                break;
            case 12:
                MonthOfName = "December";
                number_Of_DaysInMonth = 31;
        }
        System.out.print(MonthOfName + " " + year + " has " + number_Of_DaysInMonth + " days\n");
    }
}

Sample Output:

Input a month number: 2                                                                                       
Input a year: 2016                                                                                            
February 2016 has 29 days

Flowchart:

Flowchart: Java Conditional Statement Exercises - Find the number of days in a month



For more Practice: Solve these Related Problems:

  • Write a Java program to determine the number of days in a month using a switch-case structure with fall-through for common cases.
  • Write a Java program to calculate days in a month by leveraging the Java Time API and handling leap year logic.
  • Write a Java program to accept a month name and year, then output the number of days after validating the input.
  • Write a Java program to store month lengths in an array and output the correct days in a given month with special handling for February.

Go to:


PREV : Compare Floats Up to Three Decimals.
NEXT : Check Vowel or Consonant.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.