當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。