Table of Contents
Learn about how to get number of days in Month in java.
Get number of days in Month in java
Using YearMonth’s lengthOfMonth() [Java 8]
You can use YearMonth
‘s lengthOfMonth()
method to get number of days in Month in java.
- Pass Year and month to YearMonth’s
of
and create YearMonth object. - Call lengthOfMonth() on YearMonth’s object to get number of days in Month in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.arpit.java2blog; import java.time.YearMonth; public class GetNumberOfDaysInMonthJava { public static void main(String[] args) { // Get number of days in given month of the year int numberOfDaysInMonth1 = getNumberOfDaysInMonth(2019, 2); System.out.println("Number of days in Feb 2019: "+numberOfDaysInMonth1); int numberOfDaysInMonth2 = getNumberOfDaysInMonth(2020, 2); System.out.println("Number of days in Feb 2020: "+numberOfDaysInMonth2); } // Method to get number of days in month public static int getNumberOfDaysInMonth(int year,int month) { YearMonth yearMonthObject = YearMonth.of(year, month); int daysInMonth = yearMonthObject.lengthOfMonth(); return daysInMonth; } } |
Output:
Number of days in Feb 2019: 28
Number of days in Feb 2020: 29
Number of days in Feb 2020: 29
Further reading:
Using LocalDate’s lengthOfMonth [Java 8]
You can use LocalDate
‘s lengthOfMonth()
method to get number of days in Month in java.
- Pass Year and month to LocalDate’s
of
and create YearMonth object. - Call lengthOfMonth() on LocalDate’s object to get number of days in Month in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.arpit.java2blog; import java.time.LocalDate; public class GetNumberOfDaysInMonthLocalDateJava { public static void main(String[] args) { // Get number of days in given month of the year int numberOfDaysInMonth1 = getNumberOfDaysInMonth(2019, 2); System.out.println("Number of days in Feb 2019: "+numberOfDaysInMonth1); int numberOfDaysInMonth2 = getNumberOfDaysInMonth(2020, 2); System.out.println("Number of days in Feb 2020: "+numberOfDaysInMonth2); } // Method to get number of days in month public static int getNumberOfDaysInMonth(int year,int month) { // LocalDate object LocalDate date = LocalDate.of(year, month, 1); return date.lengthOfMonth(); } } |
Output:
Number of days in Feb 2019: 28
Number of days in Feb 2020: 29
Number of days in Feb 2020: 29
Using Calendar
You can use Calendar’s getActualMaximum()
to get number of days in Month in java.
- Create instance of Calendar object.
- Call Calendar’s getActualMaximum() to get number of days in Month in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; import java.util.Calendar; import java.util.GregorianCalendar; public class GetNumberOfDaysInMonthCalendarJava { public static void main(String[] args) { // Get number of days in given month of the year int numberOfDaysInMonth1 = getNumberOfDaysInMonth(2019, Calendar.FEBRUARY); System.out.println("Number of days in Feb 2019: "+numberOfDaysInMonth1); int numberOfDaysInMonth2 = getNumberOfDaysInMonth(2020, Calendar.FEBRUARY); System.out.println("Number of days in Feb 2020: "+numberOfDaysInMonth2); } // Method to get number of days in month public static int getNumberOfDaysInMonth(int year,int month) { // Create a calendar object and set year and month Calendar mycal = new GregorianCalendar(year, month, 1); int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); return daysInMonth; } } |
Output:
Number of days in Feb 2019: 28
Number of days in Feb 2020: 29
Number of days in Feb 2020: 29
That’s all about how to get number of days in Month in java
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.