Java中的 Year length() 方法及示例
Java中 Year 类的 length() 方法用于返回该年对象值的长度(以天数为单位)。 Year 只能有两种可能的长度,365(非闰年)和 366(闰年)。
语法:
public int length()
参数:此方法不接受任何参数。
返回值:它返回一个整数值,以天数表示年份的长度。
下面的程序说明了Java中 Year 的 length() 方法:
程序 1 :
// Program to illustrate the length() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Create a Year object
Year thisYear = Year.of(2016);
// Print the length of this year in Number
// of days, 366 in this case as 2016 is
// a Leap Year
System.out.println(thisYear.length());
}
}
输出:
366
方案二:
// Program to illustrate the length() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Create a Year object
Year thisYear = Year.of(1990);
// Print the length of this year in Number
// of days, 365 in this case as 1990 is not
// a Leap Year
System.out.println(thisYear.length());
}
}
输出:
365
参考:https: Java/time/Year.html#length–