Java 中 Instant 的 minusNanos() 方法
可以利用 Java 中 Instant 类的 minusNanos() 方法获取某个时刻的一个不可变副本,其中减去了一些纳秒。此方法需要一个参数,即要减去的纳秒数,它返回减去纳秒后的时刻。
演示此功能的程序如下:
示例
import java.time.*;
public class Demo {
public static void main(String[] args) {
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
System.out.println("An instant with 1000000000 nanoseconds subtracted is: " + i.minusNanos(1000000000));
}
}输 出
The current instant is: 2019-02-12T12:36:26.511Z An instant with 1000000000 nanoseconds subtracted is: 2019-02-12T12:36:25.511Z
现在让我们了解一下上述程序。
首先显示当前时刻。然后使用 minusNanos() 方法获取该时刻的一个不可变副本,其中减去了 1000000000 纳秒,并对其进行显示。演示此功能的代码片段如下:
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
System.out.println("An instant with 1000000000 nanoseconds subtracted is: " + i.minusNanos(1000000000));
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP