The from(TemporalUnit) method of Duration Class in java.time package is used to get a duration from the amount passed as the first parameter in the TemporalUnit. The TemporalUnit can be DAYS, HOURS, etc.
Syntax:
public static Duration from(TemporalUnit amount)
Parameters: This method accepts a parameter amount which is amount from which the Duration is to be found, passed as the TemporalUnit.
Return Value: This method returns a Duration representing the time of specified amount.
Exception: This method throws following unit:
- ArithmeticException: if numeric overflow occurs.
- DateTimeException: if unable to convert to a Duration
Below examples illustrate the Duration.from() method:
Example 1:
// Java code to illustrate from() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Get the amount Duration duration = Duration.ofDays(5); // Duration using from() method Duration duration1 = Duration.from(duration); System.out.println(duration1.getSeconds()); } } |
432000
Example 2:
// Java code to illustrate from() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Get the amount Duration duration = Duration.ofHours(5); // Duration using from() method Duration duration1 = Duration.from(duration); System.out.println(duration1.getSeconds()); } } |
18000
Reference: Oracle Doc
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Duration plus(Duration) method in Java with Examples
- Duration equals(Duration) method in Java with Examples
- Duration compareTo(Duration) method in Java with Examples
- Duration minus(Duration) method in Java with Examples
- Duration dividedBy(Duration) method in Java with Examples
- Duration ofSeconds(long) method in Java with Examples
- Duration ofDays(long) method in Java with Examples
- Duration ofMinutes(long) method in Java with Examples
- Duration ofHours(long) method in Java with Examples
- Duration of(long, TemporalUnit) method in Java with Examples
- Duration ofMillis(long) method in Java with Examples
- Duration ofNanos(long) method in Java with Examples
- Duration ofSeconds(long, long) method in Java with Examples
- Duration multipliedBy(long) method in Java with Examples
- Duration dividedBy(long) method in Java with Examples
- Duration negated() method in Java with Examples
- Duration abs() method in Java with Examples
- Duration hashCode() method in Java with Examples
- Duration toString() method in Java with Examples
- Duration toNanos() 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.

