java.time.Period.subtractFrom() Method Example



Description

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

Declaration

Following is the declaration for java.time.Period.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.Period.subtractFrom(Temporal temporal) method.

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.Period;

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

      Period period = Period.ofYears(2);

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

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

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

2017-03-23T11:54:51.485
2015-03-23T11:54:51.485
Advertisements