In this article, we will learn how to get number of Days in a Month from LocalDate and Calendar
1. Get Number of Days in a Month from LocalDate :
- Sometimes, we need Number of Days in a Month from current LocalDate for some business requirement
- To get number of days in a Month from LocalDate, we can use below method
- lengthOfMonth() – returns Number of Days in a Year from invoking LocalDate
- Finally, print number of days in a month to the console
1.1 Get number of Days for Feb-2016 & Feb-2022
- In the below illustration, we will form LocalDate for February–2016 and February–2022 assigning,
- Day of Month as 1
- Note:- Number of days for every month is fixed except for February which increases by a Day in a Leap Year
GetLengthOfMonthFromLocalDate2.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
public class GetLengthOfMonthFromLocalDate2 {
public static void main(String[] args) {
// 1. form LocalDate for February-2022
LocalDate localDate = LocalDate.of(2022, Month.FEBRUARY, 1);
System.out.println("LocalDate is = " + localDate);
// 1.1 Number of days in Feb-2022
System.out.println("Number of Days in "
+ localDate.getMonth() + "-"
+ localDate.getYear() + " is = \t"
+ localDate.lengthOfMonth());
// 2. form LocalDate for February-2016
LocalDate localDate2 = LocalDate.of(2016, Month.FEBRUARY, 1);
System.out.println("\nLocalDate is = " + localDate2);
// 2.1 Number of days in Feb-2016
System.out.print("Number of Days in "
+ localDate2.getMonth() + "-"
+ localDate2.getYear() + " is = \t"
+ localDate2.lengthOfMonth());
}
}
Output:
LocalDate is = 2022-02-01
Number of Days in FEBRUARY-2022 is = 28
LocalDate is = 2016-02-01
Number of Days in FEBRUARY-2016 is = 29
1.2 Get number of Days for each Month of Year-2022
- In the below illustration, we will form LocalDate for all 12 months starting from January to December assigning,
- Day of Month as 1
- Year as 2022
GetLengthOfMonthFromLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
public class GetLengthOfMonthFromLocalDate {
public static void main(String[] args) {
// 1. form LocalDate from January to December and add to List
List<LocalDate> localDates = Arrays.asList(
LocalDate.of(2022, 1, 1),
LocalDate.of(2022, 2, 1),
LocalDate.of(2022, 3, 1),
LocalDate.of(2022, 4, 1),
LocalDate.of(2022, 5, 1),
LocalDate.of(2022, 6, 1),
LocalDate.of(2022, 7, 1),
LocalDate.of(2022, 8, 1),
LocalDate.of(2022, 9, 1),
LocalDate.of(2022, 10, 1),
LocalDate.of(2022, 11, 1),
LocalDate.of(2022, 12, 1)
);
// 2. LocalDate - get number of Days in each month
System.out.print("Number of Days in each Month :- \n");
localDates.forEach(localDate -> System.out.println("Number of Days in "
+ localDate.getMonth() + "-"
+ localDate.getYear() + " is = \t"
+ localDate.lengthOfMonth()));
}
}
Output:
Number of Days in each Month :-
Number of Days in JANUARY-2022 is = 31
Number of Days in FEBRUARY-2022 is = 28
Number of Days in MARCH-2022 is = 31
Number of Days in APRIL-2022 is = 30
Number of Days in MAY-2022 is = 31
Number of Days in JUNE-2022 is = 30
Number of Days in JULY-2022 is = 31
Number of Days in AUGUST-2022 is = 31
Number of Days in SEPTEMBER-2022 is = 30
Number of Days in OCTOBER-2022 is = 31
Number of Days in NOVEMBER-2022 is = 30
Number of Days in DECEMBER-2022 is = 31
2. Get Number of Days in a Month from Calendar/Date :
- If the Java version used is lower than 8 then Calendar/Date classes can be used to get number of Days for any month of a year
2.1 Get number of Days for Feb-2016 & Feb-2022
- Below illustration prints number of days for months of February–2016 and February–2022 with,
- Day of Month field set to 1
GetLengthOfMonthFromCalendar2.java
package in.bench.resources.java8.localdate.examples;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
public class GetLengthOfMonthFromCalendar2 {
public static void main(String[] args) {
// 1. Instantiate GregorianCalendar for February-2022
Calendar calendar = new GregorianCalendar(2022, Calendar.FEBRUARY, 1);
System.out.println("Date is = " + calendar.getTime());
// 1.1 Number of days in Feb-2022
System.out.println("Number of Days in "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + "-"
+ calendar.get(Calendar.YEAR) + " is = \t"
+ calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
// 2. Instantiate GregorianCalendar for February-2016
Calendar calendar2 = new GregorianCalendar(2016, Calendar.FEBRUARY, 1);
System.out.println("\n\nDate is = " + calendar2.getTime());
// 2.1 Number of days in Feb-2016
System.out.print("Number of Days in "
+ calendar2.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + "-"
+ calendar2.get(Calendar.YEAR) + " is = \t"
+ calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
}
}
Output:
Date is = Tue Feb 01 00:00:00 IST 2022
Number of Days in February-2022 is = 28
Date is = Mon Feb 01 00:00:00 IST 2016
Number of Days in February-2016 is = 29
2.2 Get number of Days for each Month of Year-2022
- Below illustration prints number of days for each month of Year 2022 starting from January to December with,
- Day of Month field set to 1
GetLengthOfMonthFromCalendar.java
package in.bench.resources.java8.localdate.examples;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
public class GetLengthOfMonthFromCalendar {
public static void main(String[] args) {
// 1. Instantiate GregorianCalendar from January to December and add to List
List<Calendar> months = Arrays.asList(
new GregorianCalendar(2022, Calendar.JANUARY, 1),
new GregorianCalendar(2022, Calendar.FEBRUARY, 1),
new GregorianCalendar(2022, Calendar.MARCH, 1),
new GregorianCalendar(2022, Calendar.APRIL, 1),
new GregorianCalendar(2022, Calendar.MAY, 1),
new GregorianCalendar(2022, Calendar.JUNE, 1),
new GregorianCalendar(2022, Calendar.JULY, 1),
new GregorianCalendar(2022, Calendar.AUGUST, 1),
new GregorianCalendar(2022, Calendar.SEPTEMBER, 1),
new GregorianCalendar(2022, Calendar.OCTOBER, 1),
new GregorianCalendar(2022, Calendar.NOVEMBER, 1),
new GregorianCalendar(2022, Calendar.DECEMBER, 1)
);
// 2. Calendar - get number of Days in each month
System.out.println("Number of Days in each Month :- \n");
months.forEach(month -> System.out.println("Number of Days in "
+ month.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + "-"
+ month.get(Calendar.YEAR) + " is = \t"
+ month.getActualMaximum(Calendar.DAY_OF_MONTH)));
}
}
Output:
Number of Days in each Month :-
Number of Days in January-2022 is = 31
Number of Days in February-2022 is = 28
Number of Days in March-2022 is = 31
Number of Days in April-2022 is = 30
Number of Days in May-2022 is = 31
Number of Days in June-2022 is = 30
Number of Days in July-2022 is = 31
Number of Days in August-2022 is = 31
Number of Days in September-2022 is = 30
Number of Days in October-2022 is = 31
Number of Days in November-2022 is = 30
Number of Days in December-2022 is = 31
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!