Java中的 YearMonth lengthOfMonth 方法()
Java中 YearMonth 类的lengthOfMonth()方法用于返回该年月对象的月长,以天数为单位。月份长度的值在 1 到 31 的范围内。此方法还考虑闰年。
语法:
public int lengthOfMonth()
参数:此方法不接受任何参数。
返回值:它返回一个整数值,以天数表示月份的长度。
下面的程序说明了Java中 YearMonth 的 lengthOfMonth() 方法:
程序 1 :
// Program to illustrate the lengthOfMonth() 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 month in Number
// of days, 29 in this case as 2016 is
// a Leap Year
System.out.println(yearMonth.lengthOfMonth());
}
}
输出:
29
方案二:
// Program to illustrate the lengthOfMonth() 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 month in Number
// of days, 28 in this case as 1990 is
// not a Leap Year
System.out.println(yearMonth.lengthOfMonth());
}
}
输出:
28
参考:https: Java/time/YearMonth.html#lengthOfMonth–