What is Duration.plusMillis() in Java?
The plusMillis() method
plusMillis() is an instance method of the Duration class which is used to add the specified duration to the Duration object in milliseconds.
Importing the Duration class
The plusMillis() 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 Duration plusMillis(long millisToAdd)
Parameters
long millisToAdd: The number of milliseconds to add. This can be positive or negative.
Return value
This method returns a new instance of the Duration class with the number of milliseconds added.
Code
In the code below, we add 1000 milliseconds to the baseDuration object with the help of the plusMillis() method and print the new object to the console.
import java.time.Duration;public class Main{public static void main(String[] args) {Duration baseDuration = Duration.ofSeconds(3);int milliSecondsToAdd = 1000;Duration newDuration = baseDuration.plusMillis(milliSecondsToAdd);System.out.printf("%s + %s milliseconds = %s", baseDuration, milliSecondsToAdd, newDuration);System.out.println();}}