Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
from
duration

What is Duration.from() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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;

Syntax


public static Duration from(TemporalAmount amount)

Parameters

  • TemporalAmount amount: The amount of time to convert.

Return value

This method returns a new Duration object.

Example

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);
}
}
Implementation of the from() method

Explanation

  • Lines 1–2: We import the relevant packages.
  • Line 7: We create a Duration object using the from() method.
  • Line 8: We print the Duration object created in line 7.

RELATED TAGS

java
communitycreator
from
duration

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring