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