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:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
Duration duration = Duration.ofDays( 5 );
Duration duration1 = Duration.from(duration);
System.out.println(duration1.getSeconds());
}
}
|
Example 2:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
Duration duration = Duration.ofHours( 5 );
Duration duration1 = Duration.from(duration);
System.out.println(duration1.getSeconds());
}
}
|
Reference: Oracle Doc
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Nov, 2018
Like Article
Save Article