of()
is a Duration
class. It is used to obtain a Duration
object given the amount of duration and the specified unit of duration.
The of
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 static Duration of(long amount, TemporalUnit unit)
long amount
: The amount of the duration. The value can be positive or negative.TemporalUnit unit
: The unit in which the duration is measured.This method returns a Duration
object.
import java.time.Duration;import java.time.temporal.ChronoUnit;import java.time.temporal.TemporalUnit;public class Main {public static void main(String[] args){long durationAmount = 10;TemporalUnit temporalUnit = ChronoUnit.DAYS;Duration duration = Duration.of(durationAmount, temporalUnit);System.out.println("Duration object - " + duration);}}
Duration
object using the of()
method by passing the durationAmount
and temporalUnit
as arguments.Duration
object created in line 10.