Java Instant Class | plus() Method with Example

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

Instant Class plus() method

Syntax:

    public Instant plus(TemporalAmount t_amt);
    public Instant plus(long amt, TemporalUnit t_unit);
  • plus() method is available in java.time package.
  • plus(TemporalAmount t_amt) method is used to add the given amount to this Instant and return the Instant.
  • plus(long amt, TemporalUnit t_unit) method is used to add the given amount in the given unit to this Instant and return the Instant.
  • These methods may throw an exception at the time of performing addition.
    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.
    • DateTimeException: This exception may throw when getting any error during addition.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, "plus(TemporalAmount t_amt)",
    • TemporalAmount t_amt – represents the amount to be added to this Instant.
  • In the second case, "plus(long amt, TemporalUnit t_unit)",
    • long amt – represents the amount in units to be added to this Instant.
    • TemporalUnit t_unit – represents the unit to measure the given amount.

Return value:

In both the cases, the return type of the method is Instant,

  • In the first case, it returns the Instant that holds the value added the given amount to this Instant.
  • In the second case, it returns the Instant that holds the value added the given amount in unit to this Instant.

Example:

// Java program to demonstrate the example 
// of plus() method of Instant

import java.time.*;
import java.time.temporal.*;

public class PlusOfInstant {
    public static void main(String args[]) {
        long amt = 2;

        // Instantiates two Instant and
        // a Duration object
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.now();
        Duration du = Duration.ofSeconds(2);

        // Display ins1,ins2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);
        System.out.println("amt to add: " + amt);
        System.out.println("du to add: " + du);

        System.out.println();

        // Here, this method adds the given amount
        // in the given unit with this Instant and
        // returns the Instant i.e. here we are
        // adding 2 hours with Instant ins1
        Instant plus_val = ins1.plus(amt, ChronoUnit.HOURS);

        // Display plus_val
        System.out.println("ins1.plus(amt,ChronoUnit.HOURS): " + plus_val);

        // Here, this method adds the given amount
        // with this Instant and returns the Instant 
        // i.e. here we are adding 2 seconds
        // with Instant ins2
        plus_val = ins2.plus(du);

        // Display plus_val
        System.out.println("ins2.plus(du): " + plus_val);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T07:28:15.161687Z
amt to add: 2
du to add: PT2S

ins1.plus(amt,ChronoUnit.HOURS): 2006-04-03T07:10:15Z
ins2.plus(du): 2020-05-28T07:28:17.161687Z


Comments and Discussions!

Load comments ↻





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