当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Year isSupported(TemporalField)用法及代码示例


Year类的isSupported(TemporalField)方法用于检查Year类是否支持指定的字段,这意味着使用此方法,我们可以检查是否可以为指定的字段查询Year对象。

ChronoField支持的字段是:

  • YEAR_OF_ERA
  • 时代

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


用法:

public boolean isSupported(TemporalField field)

参数:领域代表要检查的字段。

返回值:如果此Year支持此字段,则此方法返回布尔值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 :2019
YEAR_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("MiliSecond Field is supported: "
                           + value); 
    } 
}
输出:
Year :2022
MiliSecond Field is supported: false

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



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Year isSupported(TemporalField) Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。