Signup/Sign In
LAST UPDATED: NOVEMBER 24, 2020

Java LocalDate lengthOfMonth() Method With Examples

Java lengthOfMonth() method is used to get the length of a month in the date. The length of a month represents the total number of days in a month. For example, we have a date 2012/01/25 then this method returns 31 as a result.

It does not take any argument but returns an integer value. The syntax of the method is given below.

Syntax

public int lengthOfMonth()

Parameters:

It does not take any argument.

Returns:

It returns an integer value.

Time for an Example

Let's take an example to get length of month. Here, we are getting October month's length which is 31. So the method returns 31 as a result.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2015, 10, 21);
		System.out.println(localDate);
		int length  = localDate.lengthOfMonth();
		System.out.println("Length of Month : "+length);
	}
}


2015-10-21
Length of Month : 31

Time for another Example

Let's take another example to understand the use of lengthOfMonth() method. Here, we are getting length of February month that has either 28 or 29. So based on the year, it returns 28 as a result.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2015, 02, 21);
		System.out.println(localDate);
		int length  = localDate.lengthOfMonth();
		System.out.println("Length of Month : "+length);
	}
}


2015-02-21
Length of Month : 28

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.