Signup/Sign In

Java LocalDate lengthOfYear() Method

Java lengthOfYear() method is used to get the length of a year in the date. The length of a year represents the total number of days in a year. For example, we have a date 2012/01/25 then this method returns 366 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 lengthOfYear()

Parameters:

It does not take any argument.

Returns:

It returns an integer value.

Time for an Example

Let's take an example to get the length of the year. Here, we are getting 2015 year's length which is 365. So the method returns 365 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.lengthOfYear();
		System.out.println("Length of Year : "+length);
	}
}


2015-02-21
Length of Year : 365

Time for another Example

Let's take another example to understand the use of lengthOfYear() method. Here, we are getting the length of a leap year that has 366 days. So based on the year, it returns 366 as a result.

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


2016-02-21
Length of Year : 366

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.