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:
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-11-20T16:55:30.00Z" );
OffsetDateTime passTemporal
= OffsetDateTime.now();
System.out.println( "Passed Value: "
+ passTemporal);
Temporal returnTemporal
= instant.adjustInto(passTemporal);
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:
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-11-17T06:50:39.00Z" );
ZonedDateTime passTemporal
= ZonedDateTime.now();
System.out.println( "Passed Value: "
+ passTemporal);
Temporal returnTemporal
= instant.adjustInto(passTemporal);
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:
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2017-11-01T16:25:00.00Z" );
Temporal passTemporal
= Instant.now();
System.out.println( "Passed Value: "
+ passTemporal);
Temporal returnTemporal
= instant.adjustInto(passTemporal);
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)
Share your thoughts in the comments
Please Login to comment...