Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
ofmillis
duration

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

ofMillis() is a staticThe methods in Java that can be called without creating an object of the class. method of the Duration class that is used to get a Duration instance representing a number of milliseconds. The seconds and nanoseconds are extracted from the specified milliseconds.

The ofMillis() 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;

Method signature

public static Duration ofMillis(long millis)

Parameters

  • long millis: The number of milliseconds. It can be positive or negative.

Return value

The method returns a Duration object.

Code

In the code below, we create two Duration class objects with a positive and negative number of milliseconds. Then we print the objects to the console.

import java.time.Duration;
public class Main{
public static void main(String[] args) {
Duration positiveMillis = Duration.ofMillis(5);
System.out.println("Positive Milliseconds - " + positiveMillis);
Duration negativeMillis = Duration.ofMillis(-5);
System.out.println("Negative Milliseconds - " + negativeMillis);
}
}

RELATED TAGS

java
communitycreator
ofmillis
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