Java 中的即时 get() 方法
可以使用 Java 中 Instant 类的 get() 方法获取 Instant 的所需 ChronoField 的值。此方法需要一个参数,即 ChronoField,它将返回作为参数传递的 ChronoField 的值。
演示此方法的代码如下 −
示例
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
public class Demo {
public static void main(String[] args) {
Instant i = Instant.now();
int micro = i.get(ChronoField.MICRO_OF_SECOND);
System.out.println("The current Instant is: " + i);
System.out.println("The MICRO_OF_SECOND Field is: " + micro);
}
}输出
The current Instant is: 2019-02-13T11:07:48.456Z The MICRO_OF_SECOND Field is: 456000
现在让我们来理解上述程序。
首先,显示当前时间点。然后使用 get() 方法获取 MICRO_OF_SECOND ChronoField 的值并显示。演示此操作的代码片段如下 −
Instant i = Instant.now();
int micro = i.get(ChronoField.MICRO_OF_SECOND);
System.out.println("The current Instant is: " + i);
System.out.println("The MICRO_OF_SECOND Field is: " + micro);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP