Java Date Time - Duration get(TemporalUnit unit) example








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;
/*from  w  ww  .j  a va 2  s .  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.