Skip to content
Related Articles

Related Articles

Improve Article

Instant adjustInto() method in Java with Example

  • Last Updated : 22 Nov, 2018

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:




// 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);
    }
}
Output:
Passed Value: 2018-11-22T09:22:17.297Z
Returned Value: 2018-11-20T16:55:30Z

Program 2:




// 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);
    }
}
Output:
Passed Value: 2018-11-22T09:22:20.995Z[Etc/UTC]
Returned Value: 2018-11-17T06:50:39Z[Etc/UTC]

Program 3:




// 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);
    }
}
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)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :