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
程序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());
}
}
输出。
365
参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#lengthOfYear-