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
相關用法
- Java YearMonth until()用法及代碼示例
- Java YearMonth with()用法及代碼示例
- Java YearMonth query()用法及代碼示例
- Java YearMonth isAfter()用法及代碼示例
- Java YearMonth lengthOfYear()用法及代碼示例
- Java YearMonth withYear()用法及代碼示例
- Java YearMonth withMonth()用法及代碼示例
- Java YearMonth plus(TemporalAmount)用法及代碼示例
- Java YearMonth isBefore()用法及代碼示例
- Java YearMonth isValidDay()用法及代碼示例
- Java YearMonth range()用法及代碼示例
- Java YearMonth isLeapYear()用法及代碼示例
- Java YearMonth plus(long,unit)用法及代碼示例
- Java YearMonth isSupported(TemporalUnit)用法及代碼示例
- Java YearMonth parse(CharSequence)用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 YearMonth isSupported(TemporalField) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。