java.time.Instant.minus() 方法

描述

java.time.Instant.minus(TemporalAmount amountToSubtract) 方法返回此瞬间的副本,并减去指定的数量。


声明

以下是 java.time.Instant.minus(TemporalAmount amountToSubtract) 方法的声明。

public Instant minus(TemporalAmount amountToSubtract)

参数

amountToSubtract − 要减去的数量,而不是 null。


返回值

基于此瞬间的 Instant 减去指定的数量,不为空。


异常

  • DateTimeException − 如果减法不能进行。

  • ArithmeticException − 如果发生数字溢出。


示例

下面的例子展示了 java.time.Instant.minus(TemporalAmount amountToSubtract) 方法的使用。

package com.tutorialspoint;

import java.time.Instant;
import java.time.Duration;

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

      Instant instant = Instant.parse("2017-02-03T10:37:30.00Z");
      Duration duration = Duration.ofHours(5); 
      Instant result = instant.minus(duration);
      System.out.println(result);  
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

2017-02-03T05:37:30Z