Java Instant Class | minusMillis() Method with Example

Instant Class minusMillis() method: Here, we are going to learn about the minusMillis() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 25, 2020

Instant Class minusMillis() method

  • minusMillis() method is available in java.time package.
  • minusMillis() method is used to subtract the given duration in milliseconds from this Instant and returns the Instant.
  • minusMillis() 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.
  • minusMillis() method may throw an exception at the time of performing subtraction.
    ArithmeticException: This exception may throw when the calculated result value exceeds the limit.

Syntax:

    public Instant minusMillis(long millis_val);

Parameter(s):

  • long millis_val – represents the milliseconds to be subtracted from this Instant.

Return value:

The return type of this method is Instant, it returns the Instant that holds the value subtracted the given milliseconds from this Instant.

Example:

// Java program to demonstrate the example 
// of minusMillis(long millis_val) method 
// of Instant

import java.time.*;

public class MinusMillisOfInstant {
    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 substract: " + millis);

        System.out.println();

        // Here, this method subtracts the given duration
        // in milliseconds from this Instant ins1 
        // i.e. here we are subtracting the given 
        // 20000 milliseconds from this ins1 

        Instant minus_millis = ins1.minusMillis(millis);

        // Display minus_millis
        System.out.println("ins1.minusMillis(millis): " + minus_millis);

        // Here, this method subtracts the given duration
        // in milliseconds from this Instant ins2 
        // i.e. here we are subtracting the given 
        // 20000 milliseconds from this ins2 

        minus_millis = ins2.minusMillis(millis);

        // Display minus_millis
        System.out.println("ins2.minusMillis(millis): " + minus_millis);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-25T22:51:33.770318Z
millis to substract: 20000

ins1.minusMillis(millis): 2006-04-03T05:09:55Z
ins2.minusMillis(millis): 2020-05-25T22:51:13.770318Z


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.