当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java YearMonth lengthOfYear()用法及代码示例


Java中的YearMonth类的lengthOfYear()方法用于返回此year-month对象的天数的天数长度。根据年份是否为leap年,年份的长度值为365或366。

用法

public int lengthOfYear()

参数:此方法不接受任何参数。


返回值:它返回一个整数值,以天数表示年份的长度。

以下程序说明了Java中YearMonth的lengthOfYear()方法:
示例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()); 
    } 
}

示例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()); 
    } 
}

参考: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#lengthOfYear-



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 YearMonth lengthOfYear() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。