Java Instant plusNanos() 方法

7 Jan 2025 | 1 分钟阅读

Java Instant 类的 plusNanos() 方法用于返回一个在指定纳秒数(duration in nanoseconds)添加后的 Instant 副本。

此实例是不可变的,不会受到此方法调用的影响。

语法

参数

nanosToAdd? 它指定要添加的纳秒数。它可以是正数或负数。

返回

  • 它返回一个基于当前 Instant 并添加了指定纳秒数的 Instant。
  • 返回值不能为 null。

Exception

DateTimeException - 如果结果超过最大或最小 Instant

ArithmeticException - 如果发生数字溢出

示例 1

输出

2018-02-03T10:15:40Z

示例 2

输出

0
70000

示例 3

输出

2018-05-01T20:57:16.073Z
2018-05-01T20:57:16.075Z
2018-05-01T20:57:16.070Z