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


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


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

ChronoField支持的字段是:

  • MONTH_OF_YEAR
  • PROLEPTIC_MONTH
  • YEAR_OF_ERA
  • 时代

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


用法:

public boolean isSupported(TemporalField field)

参数:此方法仅接受一个参数字段,该参数字段表示要检查的字段。

返回值:如果此YearMonth支持该字段,则此方法返回布尔值true,否则返回false。

以下示例程序旨在说明isSupported(TemporalField)方法:

程序1:

// Java program to demonstrate 
// YearMonth.isSupported(TemporalField) method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // Create a YearMonth object 
        YearMonth thisYearMonth = YearMonth.of(2017, 8); 
  
        // print instance 
        System.out.println("YearMonth:"
                           + thisYearMonth); 
  
        // apply isSupported() method 
        boolean value 
            = thisYearMonth.isSupported( 
                ChronoField.PROLEPTIC_MONTH); 
  
        // print result 
        System.out.println("PROLEPTIC_MONTH Field is supported by YearMonth class:"
                           + value); 
    } 
}
输出:
YearMonth:2017-08
PROLEPTIC_MONTH Field is supported by YearMonth class:true

程序2:

// Java program to demonstrate 
// YearMonth.isSupported(TemporalField) method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // Create a YearMonth object 
        YearMonth thisYearMonth = YearMonth.of(2017, 8); 
  
        // print instance 
        System.out.println("YearMonth:"
                           + thisYearMonth); 
  
        // apply isSupported() method 
        boolean value 
            = thisYearMonth.isSupported( 
                ChronoField.HOUR_OF_DAY); 
  
        // print result 
        System.out.println("HOUR_OF_DAY Field is supported by YearMonth class:"
                           + value); 
    } 
}
输出:
YearMonth:2017-08
HOUR_OF_DAY Field is supported by YearMonth class:false

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



相关用法


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