Instant 在 Java 中的 isSupported() 方法
可以使用 Java 中 Instant 类的 isSupported() 方法,检查 Instant 类是否支持 ChronoUnit。此方法需要一个参数,即要检查的 ChronoUnit。如果 Instant 类支持该 ChronoUnit,则返回 true,否则返回 false。
一个演示此方法的程序如下所示 −
示例
import java.time.*;
import java.time.temporal.ChronoUnit;
public class Demo {
public static void main(String[] args) {
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
boolean flag = i.isSupported(ChronoUnit.SECONDS);
if(flag)
System.out.println("The ChronoUnit SECONDS is supported");
else
System.out.println("The ChronoUnit SECONDS is not supported");
}
}输出
The current instant is: 2019-02-13T07:08:32.900Z The ChronoUnit SECONDS is supported
现在让我们理解一下上述程序。
首先显示当前瞬时值。然后使用 isSupported() 方法检查 Instant 类是否支持 ChronoUnit SECONDS。将返回值存储在 flag 中,并打印出相应的响应。一个演示此方法的代码片段如下 −
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
boolean flag = i.isSupported(ChronoUnit.SECONDS);
if(flag)
System.out.println("The ChronoUnit SECONDS is supported");
else
System.out.println("The ChronoUnit SECONDS is not supported");
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP