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));

更新於:2019 年 7 月 30 日

451 次瀏覽

開啟你的 職業生涯

完成該課程,獲得認證

開始
廣告
© . All rights reserved.