Duration isNegative() example

Description

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 ww  . j  a v  a  2s  .  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.





















Home »
  Java Date Time »
    java.time Reference »




Clock
DayOfWeek
Duration
Instant
LocalDate
LocalDateTime
LocalTime
Month
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset