# Determining the Number of Days in a Month

This section will show the way of counting the number of days of a month in the specified year.

Tutorials
Determining the Number of Days in a Month   This section will show the way of counting the number of days of a month in the specified year. Following program takes the year as input through the keyboard and shows the number of days in the February month of the mentioned year if the year is valid other it will show the error message and terminate the program. Code Description: GregorianCalendar(year, Calendar.FEBRUARY, 1): Above is the constructor of the GregorianCalendar class which create an instance for that. Year and month are specified in the constructor to create instance for finding the number of days in that month of the specified year. Here, the constructor takes three arguments as follows: First is the year.Second is the month (February).And third is the initial date value.Calendar.getActualMaximum(Calendar.DAY_OF_MONTH): Above method finds and returns the maximum date value in the specified month of the specific year. It returns a integer value. Here is the code of the program: import java.util.*; import java.io.*; public class GettingDaysInMonth{   public static void main(String[] args) throws IOException{   int year = 1;   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));   System.out.print("Enter year : ");   try{   year = Integer.parseInt(in.readLine());   if (year < 1900 || year > 2100){   System.out.println("Please enter year greater than 1900  and less than 2100.");   System.exit(0);   }   }   catch(NumberFormatException ne){   System.out.print(ne.getMessage() + " is not a valid entry.");   System.out.println("Please enter a four digit number.");   System.exit(0);   }   Calendar cal = new GregorianCalendar(year, Calendar.FEBRUARY, 1);   int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);   System.out.print("Number of days : " + days);   } } Download this example.

# Determining the Number of Days in a Month

Related Tutorials

Discuss: Determining the Number of Days in a Month   View All Comments

Subject (*):

Rodolfo
October 17, 2012
display the days in the month with javascript

I have hard time to get into software design, it is the first time that I'm in this class. I don't want to give up even if i don't pass this class. Any help will be great for me. Thanks