Instant 的 plusSeconds() 方法在 Java 中
可以在 Java 的 Instant 类中使用 plusSeconds() 方法获取添加了一些秒后的一个 Instant 副本,该操作是不可变的。此方法需要一个参数,即要添加的秒数,它返回添加了秒数后的 Instant。
演示此方法的一个程序如下所述:
示例
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 10 seconds added is: " + i.plusSeconds(10));
}
}输出
The current instant is: 2019-02-12T12:23:14.131Z An instant with 10 seconds added is: 2019-02-12T12:23:24.131Z
现在让我们来理解一下上述程序。
首先显示当前的 Instant。然后使用 plusSeconds() 方法获取添加了 10 秒的 Instant 副本,该副本是不可变的,并显示该副本。演示此方法的代码片段如下:
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
System.out.println("An instant with 10 seconds added is: " + i.plusSeconds(10));
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP