Skip to content
Related Articles

Related Articles

Improve Article

Period minus() method in Java with Examples

  • Last Updated : 27 Nov, 2018

The minus() method of Period class in Java is used to subtract the given amount of period from the specified period. This functions operates separately on YEAR, MONTH and DAY.

Note: Normalization is not performed. 12 months and 1 year are different.

Syntax:

public Period minus(TemporalAmount amountToSubtract)

Parameters: This method accepts a single parameter amountToSubtract, which is the amount to subtract from this period. It must not be null.

Return Value: This method returns a Period based on the given period with the requested period subtracted, and this must not be null.



Exceptions:

  • DateTimeException: This exception is returned if the specified amount has a non-ISO chronology or contains an invalid unit.
  • ArithmeticException: This exception is caught if numeric overflow occurs.

Below programs illustrate the above method:

Program 1:




// Java code to show the function minus()
// to subtract the two given periods
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodDemo {
  
    // Function to subtract two given periods
    static void subtractPeriod(Period p1, Period p2)
    {
  
        System.out.println(p1.minus(p2));
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        Period p1 = Period.of(year, months, days);
  
        // Defining second period
        int year1 = 2;
        int months1 = 7;
        int days1 = 8;
        Period p2 = Period.of(year1, months1, days1);
  
        subtractPeriod(p1, p2);
    }
}
Output:
P2Y4M2D

Program 2:




// Java code to show the function minus()
// to subtract the two given periods
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodDemo {
  
    // Function to subtract two given periods
    static void subtractPeriod(Period p1, Period p2)
    {
  
        System.out.println(p1.minus(p2));
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining second period
        int year1 = 2;
        int months1 = 7;
        int days1 = 8;
        Period p1 = Period.of(year1, months1, days1);
  
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        Period p2 = Period.of(year, months, days);
  
        subtractPeriod(p1, p2);
    }
}
Output:
P-2Y-4M-2D

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#minus-java.time.temporal.TemporalAmount-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :