字段值的范围可以使用range()
Java中Instant类中的方法获得。此方法需要一个参数,即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(); ValueRange range1 = i.range(ChronoField.MILLI_OF_SECOND); ValueRange range2 = i.range(ChronoField.MICRO_OF_SECOND); ValueRange range3 = i.range(ChronoField.NANO_OF_SECOND); System.out.println("The current Instant is: " + i); System.out.println("\nThe Range of MILLI_OF_SECOND is: " + range1); System.out.println("The Range of MICRO_OF_SECOND is: " + range2); System.out.println("The Range of NANO_OF_SECOND is: " + range3); } }
输出结果
The current Instant is: 2019-02-13T09:09:31.210Z The Range of MILLI_OF_SECOND is: 0 - 999 The Range of MICRO_OF_SECOND is: 0 - 999999 The Range of NANO_OF_SECOND is: 0 - 999999999
现在让我们了解上面的程序。
首先显示当前时刻。然后使用该range()
方法打印MILLI_OF_SECOND,MICRO_OF_SECOND和NANO_OF_SECOND的范围。演示这的代码片段如下-
Instant i = Instant.now(); ValueRange range1 = i.range(ChronoField.MILLI_OF_SECOND); ValueRange range2 = i.range(ChronoField.MICRO_OF_SECOND); ValueRange range3 = i.range(ChronoField.NANO_OF_SECOND); System.out.println("The current Instant is: " + i); System.out.println("\nThe Range of MILLI_OF_SECOND is: " + range1); System.out.println("The Range of MICRO_OF_SECOND is: " + range2); System.out.println("The Range of NANO_OF_SECOND is: " + range3);