Java中的 YearMonth lengthOfYear() 方法及示例
Java中 YearMonth 类的lengthOfYear()方法用于返回该年月对象值的年长度,以天数为单位。根据年份是否为闰年,年份长度的值是 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());
}
}
输出:
366
方案二:
// 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());
}
}
输出:
365
参考:https: Java/time/YearMonth.html#lengthOfYear–