Java Instant plusSeconds() 方法

7 Jan 2025 | 1 分钟阅读

Java Instant 类的 plusSeconds() 方法用于返回一个副本,该副本在指定的秒数持续时间后。

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

语法

参数

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

返回

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

Exception

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

ArithmeticException - 如果发生数字溢出

示例 1

输出

2018-02-03T13:02:10Z

示例 2

输出

2018-05-01T20:57:13.972Z
2018-05-02T02:30:33.972Z
2018-05-01T15:23:53.972Z