在ZonedDateTime類中,根據傳遞給它的參數,有兩種類型的isSupported()方法。
isSupported(TemporalField field)
ZonedDateTime類的isSupported()方法用於檢查ZonedDateTime類是否支持指定字段,這意味著使用此方法,我們可以檢查是否可以為指定字段查詢ZonedDateTime。
ChronoField支持的字段是:
- NANO_OF_SECOND
- MICRO_OF_SECOND
- MILLI_OF_SECOND
- INSTANT_SECONDS
- SECOND_OF_MINUTE
- NANO_OF_DAY
- MICRO_OF_DAY
- MILLI_OF_DAY
- SECOND_OF_DAY
- MINUTE_OF_HOUR
- MINUTE_OF_DAY
- HOUR_OF_AMPM
- CLOCK_HOUR_OF_AMPM
- HOUR_OF_DAY
- CLOCK_HOUR_OF_DAY
- AMPM_OF_DAY
- 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
- INSTANT_SECONDS
- OFFSET_SECONDS
所有其他ChronoField實例將返回false。
用法:
public boolean isSupported(TemporalField field)
參數:此方法接受一個要檢查的單個參數字段。
返回值:如果此日期時間支持此字段,則此方法返回布爾值true,否則返回false。
以下示例程序旨在說明isSupported()方法:
程序1:
// Java program to demonstrate
// ZonedDateTime.isSupported() method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zdt
= ZonedDateTime
.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// check ALIGNED_WEEK_OF_YEAR
// is supported in ZonedDateTime
boolean value
= zdt.isSupported(
ChronoField.ALIGNED_WEEK_OF_YEAR);
// print result
System.out.println("ALIGNED_WEEK_OF_YEAR "
+ "Field is supported:"
+ value);
}
}
輸出:
ALIGNED_WEEK_OF_YEAR Field is supported:true
isSupported(TemporalUnit unit)
ZonedDateTime類的isSupported()方法用於檢查ZonedDateTime類是否支持指定的單元,這意味著使用此方法,我們可以檢查是否可以為指定的單元查詢ZonedDateTime。
ChronoUnit支持的字段是:
- NANOS
- MICROS
- MILLIS
- SECONDS
- MINUTES
- HOURS
- HALF_DAYS
- DAYS
- WEEKS
- MONTHS
- YEARS
- DECADES
- CENTURIES
- MILLENNIA
- ERAS
所有其他ChronoUnit實例將返回false。
用法:
public boolean isSupported(TemporalUnit unit)
參數:此方法接受一個單一參數uint,即要檢查的單位。
返回值:如果此日期時間支持此字段,則此方法返回布爾值true,否則返回false。
以下示例程序旨在說明isSupported()方法:
程序1:
// Java program to demonstrate
// ZonedDateTime.isSupported() method
import java.time.*;
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime lt
= ZonedDateTime
.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// check MILLENNIA ChronoUnit supported
// in ZonedDateTime
boolean value
= lt.isSupported(ChronoUnit.MILLENNIA);
// print result
System.out.println("ChronoUnit MILLENNIA "
+ "is supported:"
+ value);
}
}
輸出:
ChronoUnit MILLENNIA is supported:true
參考:
- https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#isSupported(java.time.temporal.TemporalField)
- https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#isSupported(java.time.temporal.TemporalUnit)
相關用法
- Java Charset isSupported()用法及代碼示例
- Java Instant isSupported()用法及代碼示例
- Java LocalTime isSupported()用法及代碼示例
- Java LocalDate isSupported()用法及代碼示例
- Java MonthDay isSupported()用法及代碼示例
- Java ChronoLocalDateTime isSupported(TemporalField )用法及代碼示例
- Java YearMonth isSupported(TemporalField)用法及代碼示例
- Java ChronoLocalDateTime isSupported(TemporalUnit)用法及代碼示例
- Java ChronoZonedDateTime isSupported(TemporalUnit)用法及代碼示例
- Java Year isSupported(TemporalUnit)用法及代碼示例
- Java YearMonth isSupported(TemporalUnit)用法及代碼示例
- Java Year isSupported(TemporalField)用法及代碼示例
- Java ChronoZonedDateTime isSupported(TemporalField)用法及代碼示例
- Java ZoneOffset isSupported(TemporalField)用法及代碼示例
- Java ZonedDateTime plus()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ZonedDateTime isSupported() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。