Java LocalDate lengthOfMonth() Method
Example
Get the number of days in January and February 2026:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2026, 1, 15);
LocalDate date2 = LocalDate.of(2026, 2, 15);
System.out.println(date1.lengthOfMonth());
System.out.println(date2.lengthOfMonth());
}
}
Definition and Usage
The lengthOfMonth() method returns the number of days in the month of the date: 28, 29, 30 or 31.
It knows about leap years, so February 2024 has 29 days.
Tip: Use lengthOfYear() to get the number of days in the year (365 or 366).
Syntax
public int lengthOfMonth()
Parameter Values
None.
Technical Details
| Returns: | The number of days in the month, from 28 to 31. |
|---|
Related Pages
❮ Date and Time Methods