Java Date Time - Duration isNegative() example








Duration isNegative() checks if this duration is negative, excluding zero.

A Duration can be positive, zero or negative.

Syntax

isNegative has the following syntax.

public boolean isNegative()

Example

The following example shows how to use isNegative.

import java.time.Duration;
import java.time.LocalTime;
/*from  w w w .  j a  va  2 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.isNegative());
    
    duration = Duration.between(LocalTime.NOON,LocalTime.MIDNIGHT);
    System.out.println(duration.isNegative());
  }
}

The code above generates the following result.