OffsetDateTime adjustInto() method in Java with examples
Last Updated :
05 Nov, 2019
The adjustInto() method of OffsetDateTime class in Java adjusts the specified temporal object to have the same date and time as this object.
Syntax :
public Temporal adjustInto(Temporal temporal)
Parameter : This method accepts a single parameter temporal which specifies the target object to be adjusted, not null.
Return Value: It returns the adjusted object, not null
Errors and exception : This method throws two exceptions as described below:
- DateTimeException: if unable to make the adjustment.
- ArithmeticException: if numeric overflow occurs.
Below programs illustrate the adjustInto() method:
Program 1 :
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class GFG {
public static void main(String[] args)
{
ZonedDateTime date = ZonedDateTime.now();
System.out.println( "Current date is: " + date);
OffsetDateTime date1 = OffsetDateTime.parse( "2018-12-12T13:30:30+05:00" );
date = (ZonedDateTime)date1.adjustInto(date);
System.out.println(date);
}
}
|
Output:
Current date is: 2018-12-11T09:53:15.294Z[Etc/UTC]
2018-12-12T13:30:30Z[Etc/UTC]
Program 2 :
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class GFG {
public static void main(String[] args)
{
try {
ZonedDateTime date = ZonedDateTime.now();
System.out.println( "Current date is: " + date);
OffsetDateTime date1 = OffsetDateTime.parse( "2018-13-12T13:30:30+05:00" );
date = (ZonedDateTime)date1.adjustInto(date);
System.out.println(date);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
Current date is: 2018-12-11T09:53:24.660Z[Etc/UTC]
java.time.format.DateTimeParseException: Text '2018-13-12T13:30:30+05:00' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjuster.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...