java.time.Duration.subtractFrom() Method Example



Description

The java.time.Duration.subtractFrom(Temporal temporal) method subtracts this duration from the specified temporal object.

Declaration

Following is the declaration for java.time.Duration.subtractFrom(Temporal temporal) method.

public Temporal subtractFrom(Temporal temporal)

Parameters

temporal − the temporal object to adjust, not null.

Return Value

an object of the same type with the adjustment made, not null.

Exception

  • DateTimeException − if unable to subtract.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Duration.subtractFrom(Temporal temporal) method.

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

Let us compile and run the above program, this will produce the following result −

2017-03-09T12:50:54.005
2017-03-09T00:50:54.005000001
Advertisements