java.time.Instant.plus() 方法示例



說明

java.time.Instant.plus(long amountToAdd, TemporalUnit unit) 方法返回用指定的時間量新增的此瞬間的副本。

宣告

以下是 java.time.Instant.plus(long amountToAdd, TemporalUnit unit) 方法的宣告。

public Instant plus(long amountToAdd, TemporalUnit unit)

引數

  • amountToAdd − 要從結果中新增的單位量,可以為負數。

  • unit − 要新增的時間量單位,不能為空。

返回值

基於此瞬間的新時間,其中添加了指定時間量,不能為空。

異常

  • DateTimeException − 如果無法進行新增操作。

  • UnsupportedTemporalTypeException − 如果不支援該單位。

  • ArithmeticException − 如果發生數值溢位。

示例

以下示例演示了 java.time.Instant.plus(long amountToAdd, TemporalUnit unit) 方法的使用。

package com.tutorialspoint;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class InstantDemo {
   public static void main(String[] args) {

      Instant instant = Instant.parse("2017-02-03T10:37:30.00Z");
      Instant result = instant.plus(10, ChronoUnit.MINUTES);
      System.out.println(result);  
   }
}

讓我們編譯並執行上述程式,它將產生以下結果 −

2017-02-03T10:47:30Z
廣告
© . All rights reserved.