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) 方法:
方案一:
// 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
方案二:
// 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
参考:
Java Java )