The negated() method of ChronoPeriod interface in Java is used to return a new instance of ChronoPeriod after negating all the elements of the period YEAR, MONTH, DAY.
Syntax:
ChronoPeriod negated()
Parameters: This method does not accepts any parameter.
Return Value: This method returns a new instance of ChronoPeriod after negating each element of the period.
Exceptions: It throws an ArithmeticException. This exception is caught if numeric overflow occurs.
Below program illustrates the above method:
Program 1:
// Java code to show the function to negate all // elements of the period import java.time.*; import java.time.chrono.*; import java.time.temporal.ChronoUnit; public class ChronoPeriodClass { // Function to negate given periods static void toNegate(ChronoPeriod p1) { System.out.println(p1.negated()); } // Driver Code public static void main(String[] args) { // Defining period int year = 4; int months = 11; int days = 10; ChronoPeriod p1 = Period.of(year, months, days); toNegate(p1); } } |
P-4Y-11M-10D
Program 2:
// Java code to show the function to negate all // elements of the period import java.time.*; import java.time.chrono.*; import java.time.temporal.ChronoUnit; public class ChronoPeriodClass { // Function to negate given periods static void toNegate(ChronoPeriod p1) { System.out.println(p1.negated()); } // Driver Code public static void main(String[] args) { // Defining period int year = -4; int months = -11; int days = -10; ChronoPeriod p1 = Period.of(year, months, days); toNegate(p1); } } |
P4Y11M10D
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#negated–
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- ChronoPeriod negated() method in Java with Examples
- Period negated() method in Java with Examples
- Duration negated() method in Java with Examples
- ChronoPeriod plus() method in Java with Examples
- ChronoPeriod between() method in Java with Examples
- ChronoPeriod get() method in Java with Examples
- ChronoPeriod multipliedBy() method in Java with Examples
- ChronoPeriod toString() method in Java with Examples
- ChronoPeriod getChronology() method in Java with Examples
- ChronoPeriod subtractFrom() method in Java with Examples
- ChronoPeriod addTo() method in Java with Examples
- ChronoPeriod minus() method in Java with Examples
- ChronoPeriod hashCode() method in Java with Examples
- ChronoPeriod isZero() method in Java with Examples
- ChronoPeriod isNegative() method in Java with Examples
- ChronoPeriod getUnits() method in Java with Examples
- ChronoPeriod equals() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() 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.

