Friday, October 10, 2025
HomeLanguagesJavaInstant minusMillis() method in Java with Examples

Instant minusMillis() method in Java with Examples

The minusMillis() method of Instant class subtracts specified duration in milliseconds from this instant and returns the result as an instant object. This returned Instant is immutable.

Syntax:

public Instant minusMillis(long millisToSubtract)

Parameters: This method accepts one parameter millisToSubtract which is milliseconds to subtract.

Returns: This method returns the Instant after subtraction of milliSeconds.

Exception: This method throws following exceptions:

  • DateTimeException: if the result exceeds the maximum or minimum instant.
  • ArithmeticException: if numeric overflow occurs.

Below programs illustrate the minusMillis() method:

Program 1:




// Java program to demonstrate
// Instant.minusMillis() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T19:34:50.63Z");
  
        // subtract 64000000 MILLI_OF_SECOND
        // means 64000 seconds from this instant
        Instant returnedValue
            = instant.minusMillis(64000000);
  
        // print result
        System.out.println("Returned Instant: "
                           + returnedValue);
    }
}


Output:

Returned Instant: 2018-12-30T01:48:10.630Z

Program 2:




// Java program to demonstrate
// Instant.minusMillis() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant = Instant.now();
  
        // current Instant
        System.out.println("current instant: "
                           + instant);
  
        // subtract 36000000 MILLI_OF_SECOND
        // means 36000 seconds from this instant
        Instant returnedValue
            = instant.minusMillis(36000000);
  
        // print result
        System.out.println("Returned Instant: "
                           + returnedValue);
    }
}


Output:

current instant: 2018-11-27T05:13:12.132Z
Returned Instant: 2018-11-26T19:13:12.132Z

References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#minusMillis(long)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS