Java中的 MonthDay isSupported() 方法及示例
MonthDay类的isSupported()方法,用于检查指定字段是否被 MonthDay 类支持,即使用该方法我们可以检查是否可以针对指定字段查询此 MonthDay。
ChronoField 支持的字段有:
- MONTH_OF_YEAR
- 年
所有其他 ChronoField 实例将返回 false。
句法:
public boolean isSupported(TemporalField field)
参数:此方法接受一个参数字段,该字段是要检查的字段。
返回值:此方法返回布尔值,如果此 MonthDay 支持该字段,则返回 true,否则返回 false。
下面的程序说明了 isSupported() 方法:
方案一:
Java
// Java program to demonstrate
// MonthDay.isSupported() method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a MonthDay object
MonthDay month = MonthDay.parse("--10-12");
// apply isSupported() method
boolean value
= month.isSupported(
ChronoField.MONTH_OF_YEAR);
// print result
System.out.println("MONTH_OF_YEAR Field is supported: "
+ value);
}
}
Java
// Java program to demonstrate
// MonthDay.isSupported() method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a MonthDay object
MonthDay month = MonthDay.parse("--10-12");
// apply isSupported() method
boolean value
= month.isSupported(
ChronoField.MILLI_OF_SECOND);
// print result
System.out.println("MilliSecond Field is supported: "
+ value);
}
}
输出:
MONTH_OF_YEAR Field is supported: true
方案二:
Java
// Java program to demonstrate
// MonthDay.isSupported() method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a MonthDay object
MonthDay month = MonthDay.parse("--10-12");
// apply isSupported() method
boolean value
= month.isSupported(
ChronoField.MILLI_OF_SECOND);
// print result
System.out.println("MilliSecond Field is supported: "
+ value);
}
}
输出
MilliSecond Field is supported: false
参考资料: https: Java Java.time.temporal.TemporalField)