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.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads
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.

Advertisements

Ads
Share on Google+Share on Google+

Determining the Number of Days in a Month

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
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

Ads

 

Ads