Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Util Determining the Number of Days in a Month

Related Tutorials


 
 

Share on Google+Share on Google+

Determining the Number of Days in a Month

Advertisement
This section will show the way of counting the number of days of a month in the specified year.
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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

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
DMCA.com