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.
Syntax:
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:
Example 1:
// Java code to illustrate of() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // input amount of time long timeAmount = 5 ; // Duration using of() method Duration duration = Duration.of(timeAmount, ChronoUnit.DAYS); System.out.println(duration.getSeconds()); } } |
432000
Example 2:
// Java code to illustrate of() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // input amount of time long timeAmount = 5 ; // Duration using of() method Duration duration = Duration.of(timeAmount, ChronoUnit.HOURS); System.out.println(duration.getSeconds()); } } |
18000
Recommended Posts:
- Duration from(TemporalUnit) method in Java with Examples
- Duration get(TemporalUnit) method in Java with Examples
- Duration truncatedTo(TemporalUnit) method in Java with Examples
- Duration plus(long, TemporalUnit) method in Java with Examples
- Duration minus(long, TemporalUnit) method in Java with Examples
- Duration minus(Duration) method in Java with Examples
- Duration equals(Duration) method in Java with Examples
- Duration compareTo(Duration) method in Java with Examples
- Duration dividedBy(Duration) method in Java with Examples
- Duration plus(Duration) method in Java with Examples
- LocalDate until(Temporal,TemporalUnit) Method in Java with Examples
- ChronoZonedDateTime isSupported(TemporalUnit) method in Java with Examples
- ChronoLocalDateTime isSupported(TemporalUnit) method in Java with Examples
- ChronoLocalDate plus(long, TemporalUnit) method in Java with Examples
- ChronoZonedDateTime plus(long, TemporalUnit) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.