Java month length()方法
length() 方法是Month ENUM的一个内置方法,用于获取这个月实例中的天数。一个月的天数可以是28、30或31。闰年二月的天数是29天。
该方法接受一个布尔标志变量,该变量表示该年是否为闰年。
语法:
public int length(boolean leapYear)
参数 :该方法接受一个参数leapYear,它表示这一年是否是闰年。
返回值 :该方法返回该月的长度,即该月的天数。
以下程序说明了上述方法。
程序1 :
import java.time.*;import java.time.Month;import java.time.temporal.ChronoField; class monthEnum { public static void main(String[] args) { // Create a month instance Month month = Month.MAY; // Print the length of this Month System.out.println(month.length(false)); }}
输出。
31
程序2 :
import java.time.*;import java.time.Month;import java.time.temporal.ChronoField; class monthEnum { public static void main(String[] args) { // Create a month instance Month month = Month.FEBRUARY; // Print the length of this Month System.out.println(month.length(true)); }}输出。
29
参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#length-boolean-
