Home »
Java »
Java Reference »
Java Instant Class
Java Instant Class | plusMillis() Method with Example
Instant Class plusMillis() method: Here, we are going to learn about the plusMillis() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 26, 2020
Instant Class plusMillis() method
- plusMillis() method is available in java.time package.
- plusMillis() method is used to add the given duration in milliseconds to this Instant and return the Instant.
- plusMillis() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- plusMillis() method may throw an exception at the time of performing addition.
DateTimeException: This exception may throw when this Instant value reaches out of the min or max instant.
Syntax:
public Instant plusMillis(long millis_val);
Parameter(s):
- long millis_val – represents the milliseconds value to add to this Instant.
Return value:
The return type of this method is Instant, it returns the Instant that holds the value added the given milliseconds to this Instant.
Example:
// Java program to demonstrate the example
// of plusMillis(long millis_val) method
// of Instant
import java.time.*;
public class PlusMillisOfInstant {
public static void main(String args[]) {
long millis = 20000;
// Instantiates two Instant
Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
Instant ins2 = Instant.now();
// Display ins1, ins2 and millis
System.out.println("Instant ins1 and ins2: ");
System.out.println("ins1: " + ins1);
System.out.println("ins2: " + ins2);
System.out.println("millis to add: " + millis);
System.out.println();
// Here, this method adds the given duration
// in milliseconds with this Instant ins1
// i.e. here we are adding the given 20000
// milliseconds with this ins1
Instant plus_millis = ins1.plusMillis(millis);
// Display plus_millis
System.out.println("ins1.plusMillis(millis): " + plus_millis);
// Here, this method adds the given duration
// in milliseconds with this Instant ins2
// i.e. here we are adding the given 20000
// milliseconds with this ins2
plus_millis = ins2.plusMillis(millis);
// Display plus_millis
System.out.println("ins2.plusMillis(millis): " + plus_millis);
}
}
Output
Instant ins1 and ins2:
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-27T00:07:38.429608Z
millis to add: 20000
ins1.plusMillis(millis): 2006-04-03T05:10:35Z
ins2.plusMillis(millis): 2020-05-27T00:07:58.429608Z