Java中的 ZoneOffset isSupported(TemporalField) 方法及示例
Java.time 包中ZoneOffset 类的isSupported(TemporalField)方法用于检查作为参数传递的temporalField 是否被ZoneOffset 支持。此方法返回一个布尔值,说明相同。
句法:
public boolean isSupported(TemporalField temporalField)
参数:此方法接受参数temporalField ,如果此 ZoneOffset 实例支持,则需要检查该参数。
返回值:此方法返回一个布尔值,说明此 ZoneOffset 实例是否支持此 temporalField。
下面的示例说明了 ZoneOffset.isSupported() 方法:
示例 1:
// Java code to illustrate isSupported() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Get the ZoneOffset instance
ZoneOffset zoneOffset
= ZoneOffset.of("+05:30");
System.out.println("ZoneOffset: "
+ zoneOffset);
// Using isSupported() method
System.out.println("Is Second supported: "
+ zoneOffset.isSupported(ChronoField.OFFSET_SECONDS));
}
}
输出:
ZoneOffset: +05:30
Is Second supported: true
示例 2:
// Java code to illustrate isSupported() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Get the ZoneOffset instance
ZoneOffset zoneOffset
= ZoneOffset.ofHours(5);
System.out.println("ZoneOffset: "
+ zoneOffset);
// Using isSupported() method
System.out.println("Is Nano-Of-The-Day supported: "
+ zoneOffset.isSupported(ChronoField.NANO_OF_DAY));
}
}
输出:
ZoneOffset: +05:00
Is Nano-Of-The-Day supported: false
参考: Oracle 文档