Java MonthDay isSupported()方法及示例

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

Java MonthDay isSupported()方法及示例

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

ChronoField支持的字段是。

MONTH_OF_YEARYEAR

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

语法

public boolean isSupported(TemporalField field)

参数: 该方法接受一个参数 字段 ,即要检查的字段。

返回值: 如果该字段在这个月日被支持,该方法返回布尔值true,如果不支持,则返回false。

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

程序 1 :

// Java program to demonstrate// MonthDay.isSupported() method import java.time.*;import java.time.temporal.ChronoField; public class GFG {    public static void main(String[] args)    {        // create a MonthDay object        MonthDay month = MonthDay.parse("--10-12");         // apply isSupported() method        boolean value            = month.isSupported(                ChronoField.MONTH_OF_YEAR);         // print result        System.out.println("MONTH_OF_YEAR Field is supported: "                           + value);    }}

输出

MONTH_OF_YEAR Field is supported: true

程序2

// Java program to demonstrate// MonthDay.isSupported() method import java.time.*;import java.time.temporal.ChronoField; public class GFG {    public static void main(String[] args)    {        // create a MonthDay object        MonthDay month = MonthDay.parse("--10-12");         // apply isSupported() method        boolean value            = month.isSupported(                ChronoField.MILLI_OF_SECOND);         // print result        System.out.println("MilliSecond Field is supported: "                           + value);    }}

输出

MilliSecond Field is supported: false

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

相关推荐