Java 中 Instant 的 minusSeconds() 方法
使用 Java 中 Instant 類的 minusSeconds() 方法可以獲取對其減去一些秒時的不可變副本。該方法需要一個引數,即要減去的秒數,它返回減去秒數之後的時刻。
一個演示的程式如下 −
示例
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 subtracted is: " + i.minusSeconds(10));
}
}輸出
The current instant is: 2019-02-12T12:29:46.262Z An instant with 10 seconds subtracted is: 2019-02-12T12:29:36.262Z
現在讓我們瞭解上述程式。
首先顯示當前時刻。然後,使用 minusSeconds() 方法獲取減去 10 秒後的時刻的不可變副本並顯示該副本。演示此過程的程式碼段如下 −
Instant i = Instant.now();
System.out.println("The current instant is: " + i);
System.out.println("An instant with 10 seconds subtracted is: " + i.minusSeconds(10));
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP