可以使用get()
Java中Instant类中的方法获得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);