
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the number of days in a month of a particular year in Java?
The given task is to find the number of days in a month of a particular year. To understand this, consider that if we can determine the maximum day value in a month, we can use that as the number of days in that month.
To do this, we can use the GregorianCalendar class. It is a concrete subclass of the Calendar class, and it provides the standard calendar system used by most of the world. In Java, this GregorianCalendar can handle both the Gregorian calendar as well as Julian calendar.
Following are the different ways to find the number of days in a month of a particular year:
Number of Days in a Month using getActualMaximum()
This is one of the common ways to find the number of days in a month of a particular year is by using the getActualMaximum() method of the GregorianCalendar class. This method returns the highest value a specific calendar field can have. You can pass any field from the Calendar class as a parameter.
Syntax
Following is the syntax of the getActualMaximum() method:
public int getActualMaximum(int field)
Here, the field parameter refers to the calendar whose maximum value needs to be returned.
Example 1
In the following example, we use the getActualMaximum() method to find the maximum value (i.e., number of days) in a month of March of a particular year 2025, that the GregorianCalendar field can have:
import java.util.*; public class NoOfDaysInAMonthOfAYearTest { public static void main(String[] args) { int year = 2025; System.out.println("Given year: " + year); // GregorianCalendar for March Calendar calendar = new GregorianCalendar(year, Calendar.MARCH, 1); // Using getActualMaximum() method int numberOfDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println("The number of days in 'March' in the year " + year + " is: " + numberOfDays); } }
The above program displays the number of days in a 'March' in the year 2025:
Given year: 2025 The number of days in 'March' in the year 2025 is: 31
Example 2
The following is another example of how to find the number of days in February for each year from 2000 to 2017.
It uses a loop to go through each year in that range and the GregorianCalendar class, along with the getActualMaximum() method, to calculate the number of days in February for that specific year:
import java.util.*; public class NoOfDaysInAMonthOfAYearTest { public static void main(String[] args) { System.out.println("Number of days in February from 2000 to 2017: "); // Loop through years from 2000 to 2017 for (int year = 2000; year < 2018; year++) { // Create a GregorianCalendar for February Calendar calendar = new GregorianCalendar(year, Calendar.FEBRUARY, 1); // Get the actual maximum number of days in February int numberOfDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println("February " + year + ": " + numberOfDays + " days"); } } }
The above program produces the following output:
Number of days in February from 2000 to 2017: February 2000: 29 days February 2001: 28 days February 2002: 28 days February 2003: 28 days February 2004: 29 days February 2005: 28 days February 2006: 28 days February 2007: 28 days February 2008: 29 days February 2009: 28 days February 2010: 28 days February 2011: 28 days February 2012: 29 days February 2013: 28 days February 2014: 28 days February 2015: 28 days February 2016: 29 days February 2017: 28 days
Using YearMonth Class
Here is another way to find the number of days in a month of a particular year using the YearMonth class from java.time package. This class represents a specific year and month and provides a method called lengthOfMonth() to get the number of days in that month.
Syntax
Following is the syntax of the YearMonth class:
YearMonth ym = YearMonth.of(year, month);
Here,
- year: It is an integer representing the desired year.
- month: It is an integer from 1 (for January) to 12 (for December).
Example
This is another example of finding the number of days in a specific month, May of a particular year, 2025, using the Java YearMonth class from the java.time package. In this example, we use YearMonth.of(year, month) and lengthOfMonth() to get the total days:
import java.time.YearMonth; public class DaysInMonthExample { public static void main(String[] args) { int year = 2025; int month = 5; System.out.println("The given year is: " + year); System.out.println("The given month is: " + month); YearMonth yearMonth = YearMonth.of(year, month); int daysInMonth = yearMonth.lengthOfMonth(); System.out.println("Number of days in 'May' of year " + year + ": " + daysInMonth + " days"); } }
Following is the output of the above program:
The given year is: 2025 The given month is: 5 Number of days in 'May' of year 2025: 31 days