Java 中 Duration 的 minusNanos() 方法
Java 中可以用 Duration 類中的 minusNanos() 方法建立一個不可變的 Duration 副本,從中移除一些納秒。此方法需要一個引數,即要減去的納秒數,它會返回減去納秒後的 Duration。
示例如下:
示例
import java.time.Duration;
public class Demo {
public static void main(String[] args) {
Duration d = Duration.ofSeconds(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with 100 nano seconds removed from the duration is: " + d.minusNanos(100));
}
}輸出
The duration is: PT1S A copy with 100 nano seconds removed from the duration is: PT0.9999999S
現在,我們來理解一下上述程式。
首先,顯示 Duration。然後,使用 minusNanos() 方法,從 Duration 中移除 100 納秒,得到一個不可變的副本,並顯示出來。以下程式碼段演示了這一點:
Duration d = Duration.ofSeconds(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with 100 nano seconds removed from the duration is: " + d.minusNanos(100));
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP