當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。