Java 中 Instant plus() 方法
在 Java 中 Instant 类的 plus() 方法可用于获取不可变副本,其中向 instant 添加了时间单位。此方法需要两个参数,即要添加到 instant 的时间以及要添加时间的单位。它还返回添加了所需时间单位的 instant 不可变副本。
示范此内容的程序如下 −
示例
import java.time.*;
import java.time.temporal.ChronoUnit;
public class Demo {
public static void main(String[] args) {
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
Instant add = i.plus(2, ChronoUnit.HOURS);
System.out.println("The instant with 2 hours added is: " + add);
}
}输出
The current instant is: 2019-02-13T06:33:27.414Z The instant with 2 hours added is: 2019-02-13T08:33:27.414Z
现在,让我们来理解一下以上程序。
首先显示当前 instant。然后使用 plus() 方法获取不可变副本,其中添加了 2 小时,并显示此副本。用于示范此内容的代码片段如下 −
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
Instant add = i.plus(2, ChronoUnit.HOURS);
System.out.println("The instant with 2 hours added is: " + add);
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP