Java中的月份 maxLength() 方法
maxLength()方法是 Month ENUM 的内置方法,用于获取本月的最大天数。例如,二月可以有 28 天和 29 天,具体取决于今年是否是闰年。因此,此方法将在 2 月返回 29,因为 2 月的最大天数为 29。
语法:
public int maxLength()
参数:此方法不接受任何参数。
返回值:此方法返回本月中存在的天数的最大长度。
下面的程序说明了上述方法:
程序 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.FEBRUARY;
// Print the maximum length of this Month
System.out.println(month.maxLength());
}
}
输出:
29
方案二:
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 max length of this Month
System.out.println(month.maxLength());
}
}
输出:
31
参考:https: Java