Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Duration.getUnits() 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.

getUnits() is an instance method of the Duration class in Java that is used to retrieve the different units of time supported by the Duration object. Currently, seconds and nanoseconds are the only units of time supported.

The getUnits() method is defined in the Duration class. The Duration class is defined in the java.time package. To import the Duration class, check the following import statement.

import java.time.Duration;

Syntax


public List<TemporalUnit> getUnits()

Parameters

The method has no parameters.

Return value

This method returns a list containing the seconds and nanoseconds units.

Code

import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.List;
public class Main{
public static void main(String[] args) {
Duration duration = Duration.ofHours(100);
List<TemporalUnit> temporalUnitsSupported = duration.getUnits();
System.out.printf("The supported units are - %s", temporalUnitsSupported);
}
}

In the code above, we define a Duration object and get all the different units supported by the object with the help of getUnits() method.

RELATED TAGS

java
communitycreator

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