在LocalDate類中,根據傳遞給它的參數,有兩種類型的isSupported()方法。
isSupported(TemporalField field)
LocalDate類的isSupported()方法用於檢查LocalDate類是否支持指定的字段,這意味著使用此方法,我們可以檢查是否可以為指定字段查詢此LocalDate。
ChronoField支持的字段是:
- DAY_OF_WEEK
- ALIGNED_DAY_OF_WEEK_IN_MONTH
- ALIGNED_DAY_OF_WEEK_IN_YEAR
- DAY_OF_MONTH
- DAY_OF_YEAR
- EPOCH_DAY
- ALIGNED_WEEK_OF_MONTH
- ALIGNED_WEEK_OF_YEAR
- MONTH_OF_YEAR
- PROLEPTIC_MONTH
- YEAR_OF_ERA
- YEAR
- ERA
所有其他ChronoField實例將返回false。
用法:
public boolean isSupported(TemporalField field)
參數:此方法接受一個要檢查的單個參數字段。
返回值:如果此LocalDate支持該字段,則此方法返回布爾值true,否則返回false。
以下示例程序旨在說明isSupported()方法:
程序1:
// Java program to demonstrate
// LocalDate.isSupported() method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a LocalDate object
LocalDate lt
= LocalDate.parse("2018-11-03");
// check YEAR_OF_ERA is supported in LocalDate
boolean value
= lt.isSupported(ChronoField.YEAR_OF_ERA);
// print result
System.out.println("YEAR_OF_ERA Field is supported: "
+ value);
}
}
YEAR_OF_ERA Field is supported: true
isSupported(TemporalUnit unit)
LocalDate類的isSupported()方法用於檢查LocalDate類是否支持指定的單元,這意味著使用此方法,我們可以檢查是否可以查詢指定單元的LocalDate。
ChronoUnit支持的字段是:
- DAYS
- WEEKS
- MONTHS
- YEARS
- DECADES
- CENTURIES
- MILLENNIA
- ERAS
所有其他ChronoUnit實例將返回false。
用法:
public boolean isSupported(TemporalUnit unit)
參數:此方法接受一個單一參數uint,即要檢查的單位。
返回值:如果此LocalDate支持該字段,則此方法返回布爾值true,否則返回false。
以下示例程序旨在說明isSupported()方法:
程序1:
// Java program to demonstrate
// LocalDate.isSupported() method
import java.time.*;
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
// create a LocalDate object
LocalDate lt
= LocalDate.parse("2018-12-29");
// check CENTURIES ChronoUnit supported in LocalDate
boolean value
= lt.isSupported(ChronoUnit.CENTURIES);
// print result
System.out.println("ChronoUnit CENTURIES is supported: "
+ value);
}
}
ChronoUnit CENTURIES is supported: true
參考: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#isSupported(java.time.temporal.TemporalField),https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#isSupported(java.time.temporal.TemporalUnit)
相關用法
- Java Charset isSupported()用法及代碼示例
- Java MonthDay isSupported()用法及代碼示例
- Java ZonedDateTime isSupported()用法及代碼示例
- Java Instant isSupported()用法及代碼示例
- Java LocalTime isSupported()用法及代碼示例
- Java ChronoLocalDateTime isSupported(TemporalUnit)用法及代碼示例
- Java YearMonth isSupported(TemporalUnit)用法及代碼示例
- Java Year isSupported(TemporalUnit)用法及代碼示例
- Java YearMonth isSupported(TemporalField)用法及代碼示例
- Java Year isSupported(TemporalField)用法及代碼示例
- Java ChronoZonedDateTime isSupported(TemporalUnit)用法及代碼示例
- Java ZoneOffset isSupported(TemporalField)用法及代碼示例
- Java ChronoLocalDateTime isSupported(TemporalField )用法及代碼示例
- Java ChronoZonedDateTime isSupported(TemporalField)用法及代碼示例
- Java LocalDate now()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalDate isSupported() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。