在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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。