java.time.Duration.get() Method Example



Description

The java.time.Duration.get() method gets the value of the requested unit.

Declaration

Following is the declaration for java.time.Duration.get() method.

public long get(TemporalUnit unit)

Parameters

unit − the TemporalUnit for which to return the value.

Return Value

the long value of the unit.

Exception

  • DateTimeException − if the unit is not supported.

  • UnsupportedTemporalTypeException − if the unit is not supported.

Example

The following example shows the usage of java.time.Duration.get() method.

package com.tutorialspoint;

import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class DurationDemo {
   public static void main(String[] args) {

      Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX);  
      System.out.println(duration.get(ChronoUnit.SECONDS));
   }
}

Let us compile and run the above program, this will produce the following result −

43199
Advertisements