Skip to content
Related Articles

Related Articles

YearMonth lengthOfYear() method in Java with Examples
  • Last Updated : 05 May, 2020

The lengthOfYear() method of YearMonth class in Java is used to return the length of year of this year-month object’s value in number of days. The value of the length of a year is either 365 or 366 based on whether the Year is leap or not.

Syntax:

public int lengthOfYear()

Parameter: This method does not accepts any parameter.

Return Value: It returns an integer value denoting the length of year in number of days.

Below programs illustrate the lengthOfYear() method of YearMonth in Java:
Program 1:






// Program to illustrate the lengthOfYear() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create YearMonth object
        YearMonth yearMonth = YearMonth.of(2016, 2);
  
        // Print the length of year in Number
        // of days, 366 in this case as 2016 is
        // a Leap Year
        System.out.println(yearMonth.lengthOfYear());
    }
}
Output:
366

Program 2:




// Program to illustrate the lengthOfYear() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create YearMonth object
        YearMonth yearMonth = YearMonth.of(1990, 2);
  
        // Print the length of year in Number
        // of days, 365 in this case as 1990 is
        // not a Leap Year
        System.out.println(yearMonth.lengthOfYear());
    }
}
Output:
365

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#lengthOfYear–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :