Java Date Time - Duration of(long amount, TemporalUnit unit) example








Duration of(long amount, TemporalUnit unit) creates a Duration representing an amount in the specified unit.

The parameters represent a phrase like '6 Hours'.

Only a subset of units are accepted by this method.

Syntax

of has the following syntax.

public static Duration of(long amount,   TemporalUnit unit)

Example

The following example shows how to use of.

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

public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.of(10,ChronoUnit.DAYS);
    System.out.println(duration);
  }
}

The code above generates the following result.