Java Instant Class | adjustInto() Method with Example

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

Instant Class adjustInto() method

  • adjustInto() method is available in java.time package.
  • adjustInto() method is used to adjust this Instant value into the given Temporal object.
  • adjustInto() 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.
  • adjustInto() method may throw an exception at the time of adjusting Instant.
    • DateTimeException: This exception may throw when the given parameter couldn't adjust this Instant.
    • ArithmeticException: This exception may throw when the calculated result exceeds the length of this Instant.

Syntax:

    public Temporal adjustInto(Temporal temp);

Parameter(s):

  • Temporal temp – represents the temporal object to adjust this Instant.

Return value:

The return type of this method is Temporal, it returns Temporal object that adjust this Instant.

Example:

// Java program to demonstrate the example 
// of Instant adjustInto(Temporal temp) method 
// of Instant

import java.time.*;
public class AdjustIntoOfInstant {
    public static void main(String args[]) {

        // Instantiates two Instant,
        // one OffsetDateTime and one
        // ZonedDateTime object
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.parse("2007-06-05T10:20:30.00Z");
        OffsetDateTime off_da_time = OffsetDateTime.now();
        ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-07-06T20:25:30.213+03:00[Africa/Asmara]");

        // Display ins1,ins2,off_da_time
        // and zo_da_time
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);
        System.out.println();
        System.out.println("OffsetDateTime off_da_time: ");
        System.out.println("off_da_time: " + off_da_time);
        System.out.println();
        System.out.println("ZonedDateTime zo_da_time: ");
        System.out.println("zo_da_time: " + zo_da_time);

        System.out.println();

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this Instant(ins1) into the
        // given object OffsetDateTime(off_da_time)
        off_da_time = (OffsetDateTime) ins1.adjustInto(off_da_time);

        // Display updated off_da_time
        System.out.println("ins1.adjustInto(off_da_time): " + off_da_time);

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this Instant(ins2) into the
        // given object ZonedDateTime(zo_da_time)
        zo_da_time = (ZonedDateTime) ins2.adjustInto(zo_da_time);

        // Display updated zo_da_time
        System.out.println("ins2.adjustInto(zo_da_time): " + zo_da_time);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2007-06-05T10:20:30Z

OffsetDateTime off_da_time: 
off_da_time: 2020-05-21T21:15:29.211673Z

ZonedDateTime zo_da_time: 
zo_da_time: 2008-07-06T20:25:30.213+03:00[Africa/Asmara]

ins1.adjustInto(off_da_time): 2006-04-03T05:10:15Z
ins2.adjustInto(zo_da_time): 2007-06-05T13:20:30+03:00[Africa/Asmara]


Comments and Discussions!

Load comments ↻





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