Instant plusSeconds() method in Java with Examples

Last Updated : 28 Nov, 2018
The plusSeconds() method of Instant class adds seconds value passed as parameter to this instant and return the result as an instant object. This returned Instant is immutable. Syntax:
public Instant plusSeconds(long secondsToAdd)
Parameters: This method accepts one parameter secondsToAdd which is seconds to be added. Returns: This method returns Instant after adding the 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 plusSeconds() method: Program 1: Java
// Java program to demonstrate
// Instant.plusSeconds() method

import java.time.*;

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

        // create a Instant object
        Instant instant
            = Instant.parse("2018-10-28T19:34:50.63Z");

        // print Instant
        System.out.println("Instant: "
                           + instant);

        // addition of 84000 seconds to this instant
        Instant returnedValue
            = instant.plusSeconds(84000);

        // print result
        System.out.println("Returned Instant: "
                           + returnedValue);
    }
}
Output:
Instant: 2018-10-28T19:34:50.630Z
Returned Instant: 2018-10-29T18:54:50.630Z
Program 2: Java
// Java program to demonstrate
// Instant.plusSeconds() 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);

        // addition of 930000 seconds
        // to this instant
        Instant returnedValue
            = instant.plusSeconds(930000);

        // print result
        System.out.println("Returned Instant: "
                           + returnedValue);
    }
}
Output:
Current instant: 2018-11-28T05:39:46.572Z
Returned Instant: 2018-12-08T23:59:46.572Z
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#plusSeconds(long)
Comment