from()
is a static method of the Duration
class. It is used to obtain an instance of the Duration
class based on the provided TemporalAmount
.
A TemporalAmount
represents an amount of time that may be date-based or time-based.
Check the following import statement to import the Duration
class.
import java.time.Duration;
public static Duration from(TemporalAmount amount)
TemporalAmount amount
: The amount of time to convert.This method returns a new Duration
object.
import java.time.Duration;import java.time.temporal.ChronoUnit;public class Main {public static void main(String[] args){Duration duration = Duration.from(ChronoUnit.HOURS.getDuration());System.out.println("Duration object - " + duration);}}
Duration
object using the from()
method.Duration
object created in line 7.