Java year isSupported(TemporalField)方法及示例

来源:这里教程网 时间:2026-02-17 21:17:59 作者:

Java year isSupported(TemporalField)方法及示例

Year 类的 isSupported(TemporalField) 方法用于检查指定的字段是否被Year类所支持,使用这个方法我们可以检查这个Year对象是否可以被查询到指定的字段。

ChronoField支持的字段是。

YEAR_OF_ERAYEARERA

所有其他ChronoField实例将返回false。

语法

public boolean isSupported(TemporalField field)

参数: 该方法只接受一个参数字段,代表要检查的字段。
返回值: 如果该字段在该年支持,该方法返回布尔值true,如果不支持则返回false。

下面的程序说明了isSupported(TemporalField)方法。

程序1 :

// 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);    }}

输出

Year :2019YEAR_OF_ERA Field is supported by Year class: true

程序2

// 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 :2022MilliSecond Field is supported: false

References:
https://docs.oracle.com/javase/10/docs/api/java/time/Year.html#isSupported(java.time.temporal.TemporalField)

相关推荐