Java 中 Instant 的 minusNanos() 方法


可以利用 Java 中 Instant 類的 minusNanos() 方法獲取某個時刻的一個不可變副本,其中減去了一些納秒。此方法需要一個引數,即要減去的納秒數,它返回減去納秒後的時刻。

演示此功能的程式如下:

示例

 線上演示

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 1000000000 nanoseconds subtracted is: " + i.minusNanos(1000000000));
   }
}

輸   出

The current instant is: 2019-02-12T12:36:26.511Z
An instant with 1000000000 nanoseconds subtracted is: 2019-02-12T12:36:25.511Z

現在讓我們瞭解一下上述程式。

首先顯示當前時刻。然後使用 minusNanos() 方法獲取該時刻的一個不可變副本,其中減去了 1000000000 納秒,並對其進行顯示。演示此功能的程式碼片段如下:

Instant i = Instant.now();
System.out.println("The current instant is: " + i);
System.out.println("An instant with 1000000000 nanoseconds subtracted is: " + i.minusNanos(1000000000));

更新於: 2019 年 7 月 30 日

125 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.