plusMillis() Method in Java with Examples

In this tutorial, we will learn the plusMillis() method in Java with examples. The plusMillis() method adds the specified number of milliseconds to the time value. This method can be used in many classes like Duration and Instant. For this, we have to import the java.time package.

plusMillis() Method of Instant Class

The plusMillis() method of Instant class adds the given number of milliseconds to the Instant. The result is in the form of an object of that class i.e the Instant class.
This method accepts only one parameter, i.e the number of milliseconds to add to the object.

The syntax of the plusMillis() method in Instant is:

public Instant plusMillis(long number_of_milliseconds_to_be_added)

Note: The parameter can be negative. Then it will deduct the given number of milliseconds from the object.

This method throws two exceptions:

  1. DateTimeException: when the maximum and minimum instant is exceeded by the result
  2. ArithmeticException: in case of arithmetic overflow

plusMillis() Method of Instant Class in Java with Example

I have provided the code of the plusMillis() method of instant class.

import java.time.*;

public class plusM{
    public static void main(String[] args) {
      Instant inst = Instant.parse("2020-05-31T12:34:56.78Z");
      System.out.println("Instant: "+inst);
      Instant val = inst.plusMillis(480000); 
      System.out.println("Instant after plusMillis() method: " + val); 
   }
}

Here, we make an Instant object and parse any date and time. Next, we call the plusMillis() function and provide the parameter. Finally, we print the result.

The output will be as follows:

Instant: 20-05-31T12:34:56.780Z
Instant after plusMillis() method: 20-05-31T12:42:56:780Z

Note: We can also give a negative parameter. It will, in turn, subtract the given number of milliseconds.

import java.time.*;

public class Main{
   public static void main(String[] args) {
       
      Instant inst = Instant.now();
      System.out.println("Instant: "+inst);
      Instant val = inst.plusMillis(-480000); 
      System.out.println("Instant after plusMillis() method: " + val);
      
   }
}

plusMillis() Method of Duration Class

The plusMillis() method of Duration class adds the given number of milliseconds to the present Duration. Hence, the result is a Duration based on this duration, plus the parameter.
This method accepts only one parameter, i.e the number of milliseconds to add to the object.

The syntax of the plusMillis() method in Duration is:

public Duration plusMillis(long number_of_milliseconds_to_be_added)

Note: The parameter can be negative. Then it will deduct the given number of milliseconds from the object.

plusMillis() Method of Duration Class in Java with Example

The following is the code of the plusMillis() method of Duration class.

import java.time.*;

public class plusM{
   public static void main(String[] args) {

      Duration dur = Duration.ofSeconds(5);
      System.out.println("Duration: "+dur.toMillis());
      Duration req = dur.plusMillis(105);
      System.out.println("Duration after plusMillis() method: "+req.toMillis());
    
    }
}

We can also give negative parameters, similar to that of Instant class.

Leave a Reply

Your email address will not be published. Required fields are marked *