Java month isSupported()方法

来源:这里教程网 时间:2026-02-17 20:57:44 作者:

Java month isSupported()方法

isSupported() 方法是Month ENUM的一个内置方法,用于检查指定字段是否被支持。该方法接受一个字段作为参数,并根据该字段是否被支持返回真或假。

语法:

public boolean isSupported(TemporalField field)

参数 :该方法接受一个单一的参数字段,要检查它是否被支持。

返回值 :如果该字段被支持,该方法返回一个布尔值True,否则返回False。

下面的程序说明了上述方法。

程序1 :

import java.time.*;import java.time.Month;import java.time.temporal.ChronoField;  class monthEnum {    public static void main(String[] args)    {        // Create a month instance        Month month = Month.of(5);          // check if the field is valid        if (month.isSupported(ChronoField.MONTH_OF_YEAR))            System.out.println("This field is supported!");        else            System.out.println("This field is not supported!");    }}

输出。

This field is supported!

程序2 :

import java.time.*;import java.time.Month;import java.time.temporal.ChronoField;  class monthEnum {    public static void main(String[] args)    {        // Create a month instance        Month month = Month.of(5);          // check if the field is valid        if (month.isSupported(ChronoField.DAY_OF_WEEK))            System.out.println("This field is supported!");        else            System.out.println("This field is not supported!");    }}

输出。

This field is not supported!

参考资料 : https://www.tutorialspoint.com/javatime/javatime_month_issupported.htm

相关推荐