ofMillis()
is a 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;
public static Duration ofMillis(long millis)
long millis
: The number of milliseconds. It can be positive or negative.The method returns a Duration
object.
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);}}