Java YearMonth isSupported(TemporalField)方法及示例
YearMonth 类的 isSupported(TemporalField) 方法是用来检查指定的字段是否被YearMonth类所支持。
ChronoField支持的字段是。
- MONTH_OF_YEAR
- PROLEPTIC_MONTH
- YEAR_OF_ERA
- YEAR
- 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)