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