Saturday, June 13, 2026
HomeLanguagesJavaInstant minusSeconds() method in Java with Examples

Instant minusSeconds() method in Java with Examples

The minusSeconds() method of Instant class subtracts specified second value from this instant and return the result as an instant object. This instant is immutable.
Syntax: 
 

public Instant minusSeconds(long secondsToSubtract)

Parameters: This method accepts one parameter secondsToSubtract which is seconds to be subtracted.
Returns: This method returns Instant after subtraction of seconds.

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 minusSeconds() method:
Program 1: 
 

Java




// Java program to demonstrate
// Instant.minusSeconds() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-10-30T09:05:55.13Z");
 
        // current Instant
        System.out.println("Initialize instant: "
                           + instant);
 
        // subtract 4300 seconds from this instant
        Instant returnedValue
            = instant.minusSeconds(4300);
 
        // print result
        System.out.println("Returned Instant: "
                           + returnedValue);
    }
}


Output

Initialize instant: 2018-10-30T09:05:55.130Z
Returned Instant: 2018-10-30T07:54:15.130Z

Program 2: 
 

Java




// Java program to demonstrate
// Instant.minusSeconds() 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 54000 from this instant
        Instant returnedValue
            = instant.minusSeconds(54000);
 
        // print result
        System.out.println("Returned Instant: "
                           + returnedValue);
    }
}


Output: 

Current instant: 2018-11-27T06:44:04.901Z
Returned Instant: 2018-11-26T15:44:04.901Z

 

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

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS