Duration of(long, TemporalUnit) method in Java with Examples
The of(long, TemporalUnit) method of Duration Class in java.time package is used to get a duration of the amount passed as the first parameter in the TemporalUnit passed as the second parameter. The TemporalUnit can be DAYS, HOURS, etc.
public static Duration of(long amount, TemporalUnit unit)
Parameters: This method accepts two parameters:
- amount: which is the number of Seconds. It can be positive or negative.
- unit: which is the TemporalUnit in which the unit is to be specified.
Return Value: This method returns a Duration representing the time in specified unit format.
Exception: This method throws following unit:
- ArithmeticException: if the input Seconds exceeds the capacity of Duration.
- DateTimeException: if the period unit has an estimated duration
Below examples illustrate the Duration.of() method:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.