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.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.
Determining the Number of Days in a Month
Tutorials
- Java util package Examples
- Java Util Package - Utility Package of Java
- Generating Random Number
- Breaking the String into Words
- Listing all available Locales
- Setting the default Locale
- Associate a value with an object
- Comparing Arrays
- Shuffling the Element of a List or Array
- Collections Framework
- Collection Iterate Example
- Converting Collection to an Array
- Converting an Array to a Collection
- Implement the Queue in Java
- Implementing a Stack in Java
- Implementing a Least-Recently-Used(LRU) Cache
- Creating a Copy of a Collection
- Making a Collection Read-Only
- Sorting elements of a Collection
- Creating a Hash Table
- Finding an Element in a Sorted Array
- Read the Key-Value of Property File in Java
- Get All Keys and Values of the Properties files in Java
- Write the Keys and Values of the Properties files in Java
- Scheduling a Timer Task
- Getting the Current Time
- Getting Information of All Available Time Zones
- Getting the current date
- Determining the Number of Days in a Month
- Comparing Dates in Java
- Determining the actual age from date of birth in Java
- Determining If a Year is a Leap Year in Java
- Determining the Day-of-Week for a Particular Date
- What is a JAR file in Java
- Creating a JAR file in Java
- Viewing contents of a JAR File
- Listing the Main Attributes in a JAR File Manifest
- Creating a ZIP file in Java
- Listing Contents of a ZIP File
- Unzip a ZIP File