The get()
is an instance method of the Duration
class which is used to get the seconds and nanosecond part of the Duration
object.
Any other units of time throw an exception.
The get()
method is defined in the Duration
class. The Duration
class is defined in the java.time
package. To import the Duration
class check the following import statement.
import java.time.Duration;
public long get(TemporalUnit unit)
TemporalUnit unit
: The unit for which to return the value.This method returns the long
value of the specified unit.
import java.time.Duration;import java.time.temporal.ChronoUnit;public class Main{public static void main(String[] args) {Duration duration = Duration.ofSeconds(5, 100);System.out.printf("Seconds in %s - %s", duration, duration.get(ChronoUnit.SECONDS));System.out.println();System.out.printf("Nanoseconds in %s - %s", duration, duration.get(ChronoUnit.NANOS));}}
Duration
object having 5
seconds and 100
nanoseconds.Duration
object created in line 7 with the help of get()
method with ChronoUnit.SECONDS
as the unit of time.Duration
object created in line 7 with the help of get()
method with ChronoUnit.NANOS
as the unit of time.