java.time.Duration.subtractFrom() 方法示例



描述

java.time.Duration.subtractFrom(Temporal temporal) 方法從指定的時間物件中減去此持續時間。

宣告

以下是 java.time.Duration.subtractFrom(Temporal temporal) 方法的宣告。

public Temporal subtractFrom(Temporal temporal)

```java

引數

temporal - 要調整的時間物件,非空。

返回值

調整後的相同型別的物件,非空。

  • 異常

  • DateTimeException - 如果無法減去。

ArithmeticException - 如果發生數字溢位。

示例

package com.tutorialspoint;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;

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

      Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX); 

      LocalDateTime date = LocalDateTime.now();
      System.out.println(date);  

      date = (LocalDateTime)duration.subtractFrom(date);
      System.out.println(date);  
   }
}

現場演示

2017-03-09T12:50:54.005
2017-03-09T00:50:54.005000001
列印頁面
廣告
© . All rights reserved.