ChronoPeriod minus() method in Java with Examples
The minus() method of ChronoPeriod class in Java is used to subtract the given amount of period from the specified period. This function operates separately on YEAR, MONTH and DAY.
Note: Normalization is not performed. 12 months and 1 year are different.
Syntax:
ChronoPeriod 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 ChronoPeriod 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.*; import java.time.chrono.*; import java.time.temporal.ChronoUnit; public class ChronoPeriodDemo { // Function to subtract two given periods static void subtractChronoPeriod(ChronoPeriod p1, ChronoPeriod 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; ChronoPeriod p1 = Period.of(year, months, days); // Defining second period int year1 = 2; int months1 = 7; int days1 = 8; ChronoPeriod p2 = Period.of(year1, months1, days1); subtractChronoPeriod(p1, p2); } } |
P2Y4M2D
Program 2:
// Java code to show the function minus() // to subtract the two given periods import java.time.*; import java.time.chrono.*; import java.time.temporal.ChronoUnit; public class ChronoPeriodDemo { // Function to subtract two given periods static void subtractChronoPeriod(ChronoPeriod p1, ChronoPeriod 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; ChronoPeriod p1 = Period.of(year1, months1, days1); // Defining first period int year = 4; int months = 11; int days = 10; ChronoPeriod p2 = Period.of(year, months, days); subtractChronoPeriod(p1, p2); } } |
P-2Y-4M-2D
Recommended Posts:
- ChronoPeriod get() method in Java with Examples
- ChronoPeriod plus() method in Java with Examples
- ChronoPeriod between() method in Java with Examples
- ChronoPeriod toString() method in Java with Examples
- ChronoPeriod addTo() method in Java with Examples
- ChronoPeriod equals() method in Java with Examples
- ChronoPeriod getChronology() method in Java with Examples
- ChronoPeriod multipliedBy() method in Java with Examples
- ChronoPeriod negated() method in Java with Examples
- ChronoPeriod isNegative() method in Java with Examples
- ChronoPeriod negated() method in Java with Examples
- ChronoPeriod subtractFrom() method in Java with Examples
- ChronoPeriod hashCode() method in Java with Examples
- ChronoPeriod getUnits() method in Java with Examples
- ChronoPeriod isZero() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


