Java Date Time - Duration abs() example








Duration abs() returns a copy of this duration with a positive length.

For example, PT-1.3S will be returned as PT1.3S.

Syntax

abs has the following syntax.

public Duration abs()

Example

The following example shows how to use abs.

import java.time.Duration;
import java.time.LocalTime;
//  w w w.  j a v  a2 s.c  o  m
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.abs());

  }
}

The code above generates the following result.