java.time.Duration.negated() Method Example



Description

The java.time.Duration.negated() method returns a copy of this duration with the length negated.

Declaration

Following is the declaration for java.time.Duration.negated() method.

public Duration negated()

Return Value

a Duration based on this duration with the amount negated, not null.

Exception

ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Duration.negated() method.

package com.tutorialspoint;

import java.time.Duration;

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

      Duration duration = Duration.ofSeconds(5);
      System.out.println(duration.getSeconds());
      Duration duration1 = duration.negated();
      System.out.println(duration1.getSeconds());
   }
}

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

5
-5
Advertisements