# Duration negated() method in Java

Java 8ProgrammingObject Oriented Programming

#### Complete Java Programming Fundamentals With Sample Projects

98 Lectures 7.5 hours

#### Get your Java dream job! Beginners interview preparation

85 Lectures 6 hours

#### Core Java bootcamp program with Hands on practice

Featured

99 Lectures 17 hours

An immutable copy of a duration where the duration is negated can be obtained using the negated() method in the Duration class in Java. This method requires no parameters and it returns the negated duration. Also, if a numeric overflow occurs the ArithmeticException is thrown.

A program that demonstrates this is given as follows −

## Example

Live Demo

import java.time.Duration;
public class GFG {
public static void main(String[] args) {
Duration d = Duration.ofHours(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with negated duration is: " + d.negated());
}
}

## Output

The duration is: PT1H
A copy with negated duration is: PT-1H

Now let us understand the above program.

An immutable copy of the duration where the duration is negated is obtained using the negated() method. Then this is displayed. A code snippet that demonstrates this is as follows −

Duration d = Duration.ofHours(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with negated duration is: " + d.negated());
Updated on 30-Jul-2019 22:30:25