Duration get(TemporalUnit unit) example

Description

Duration get(TemporalUnit unit) gets the value of the requested unit.

This returns a value for each of the two supported units, SECONDS and NANOS. All other units throw an exception.

Syntax

get has the following syntax.


public long get(TemporalUnit unit)

Example

The following example shows how to use get.


import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
//w  ww .jav a  2s . c  om
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.get(ChronoUnit.SECONDS));
    
  }
}

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