Instant adjustInto() method in Java with Example

The adjustInto(Temporal temporal) method of Instant class adjusts the passed temporal object to have this instant on which this method is applied.

Syntax:

public Temporal adjustInto(Temporal temporal)

Parameters: This method accepts a parameter temporal which is the target temporal object to be adjusted. It should not be null.



Return Value: This method returns the adjusted temporal object.

Exception: This method throws following exceptions:

  • DateTimeException if this method is unable to make the adjustment.
  • ArithmeticException if numeric overflow occurs while adjusting.

Below programs illustrate the Instant.adjustInto() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Instant.adjustInto() method
  
import java.time.*;
import java.time.temporal.Temporal;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2018-11-20T16:55:30.00Z");
  
        // create a Temporal object
        // which is equal to OffsetDateTime object
        OffsetDateTime passTemporal
            = OffsetDateTime.now();
  
        // print passed Value
        System.out.println("Passed Value: "
                           + passTemporal);
  
        // apply adjustInto method
        // to adjust OffsetDateTime Temporal
        // to instant object
        Temporal returnTemporal
            = instant.adjustInto(passTemporal);
  
        // print results
        System.out.println("Returned Value: "
                           + (OffsetDateTime)returnTemporal);
    }
}

chevron_right


Output:

Passed Value: 2018-11-22T09:22:17.297Z
Returned Value: 2018-11-20T16:55:30Z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Instant.adjustInto() method
  
import java.time.*;
import java.time.temporal.Temporal;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2018-11-17T06:50:39.00Z");
  
        // create a Temporal object
        // which is equal to ZonedDateTime object
        ZonedDateTime passTemporal
            = ZonedDateTime.now();
  
        // print passed Value
        System.out.println("Passed Value: "
                           + passTemporal);
  
        // apply adjustInto method
        // to adjust ZonedDateTime Temporal
        // to instant object
        Temporal returnTemporal
            = instant.adjustInto(passTemporal);
  
        // print results
        System.out.println("Returned Value: "
                           + (ZonedDateTime)returnTemporal);
    }
}

chevron_right


Output:

Passed Value: 2018-11-22T09:22:20.995Z[Etc/UTC]
Returned Value: 2018-11-17T06:50:39Z[Etc/UTC]

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Instant.adjustInto() method
  
import java.time.*;
import java.time.temporal.Temporal;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an instance object
        Instant instant
            = Instant.parse("2017-11-01T16:25:00.00Z");
  
        // create a Temporal object
        // which is equal to Instant object
        // with current Instant
        Temporal passTemporal
            = Instant.now();
  
        // print passed Value
        System.out.println("Passed Value: "
                           + passTemporal);
  
        // apply adjustInto method to adjust Temporal
        // to this instant object
        Temporal returnTemporal
            = instant.adjustInto(passTemporal);
  
        // print results
        System.out.println("Returned Value: "
                           + returnTemporal);
    }
}

chevron_right


Output:

Passed Value: 2018-11-22T09:22:23.298Z
Returned Value: 2017-11-01T16:25:00Z

References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#adjustInto(java.time.temporal.Temporal)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.