Home »
Java »
Java Reference »
Java LocalDateTime Class
Java LocalDateTime Class | adjustInto() Method with Example
LocalDateTime Class adjustInto() method: Here, we are going to learn about the adjustInto() method of LocalDateTime Class with its syntax and example.
Submitted by Preeti Jain, on June 04, 2020
LocalDateTime Class adjustInto() method
- adjustInto() method is available in java.time package.
- adjustInto() method is used to adjust this LocalDateTime object into the given Temporal object or in other words we can say the adjusted object is to have the same date and time represented by this 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 Date-Time.
- DateTimeException – This exception may throw when the given parameter couldn't adjust this LocalDateTime.
- ArithmeticException – This exception may throw when the calculated result exceeds the length of this Date.
Syntax:
public Temporal adjustInto(Temporal temp);
Parameter(s):
- Temporal temp – represents the temporal object to adjust this LocalDateTime.
Return value:
The return type of this method is Temporal, it returns the Temporal object that holds the value adjusted this object date-time into the given object.
Example:
// Java program to demonstrate the example
// of adjustInto(Temporal temp) method
// of LocalDateTime
import java.time.*;
public class AdjustIntoOfLocalDateTime {
public static void main(String args[]) {
// Instantiates two LocalDateTime
// and a ZonedDateTime object
LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10");
LocalDateTime da_ti2 = LocalDateTime.now();
ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-08-08T20:20:20.213+03:00[Africa/Asmara]");
// Display da_ti1, da_ti2
// and zo_da_time
System.out.println("LocalDateTime da_ti1 and da_ti2: ");
System.out.println("da_ti1: " + da_ti1);
System.out.println("da_ti2: " + da_ti2);
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 LocalDateTime(da_ti1)
// into the given object ZonedDateTime(zo_da_time)
// and the given object is to have same
// date and time as this object da_ti1
zo_da_time = (ZonedDateTime) da_ti1.adjustInto(zo_da_time);
// Display updated zo_da_time
System.out.println("da_ti1.adjustInto(zo_da_time): " + zo_da_time);
// Here, this method adjusts this object
// into the given object i.e. here we are
// adjusting this LocalDateTime(da_ti2)
// into the given object ZonedDateTime(zo_da_time)
// and the given object is to have same
// date and time as this object da_ti2
zo_da_time = (ZonedDateTime) da_ti2.adjustInto(zo_da_time);
// Display updated zo_da_time
System.out.println("da_ti2.adjustInto(zo_da_time): " + zo_da_time);
}
}
Output
LocalDateTime da_ti1 and da_ti2:
da_ti1: 2005-10-05T10:10:10
da_ti2: 2020-06-04T07:51:48.984048
ZonedDateTime zo_da_time:
zo_da_time: 2008-08-08T20:20:20.213+03:00[Africa/Asmara]
da_ti1.adjustInto(zo_da_time): 2005-10-05T10:10:10+03:00[Africa/Asmara]
da_ti2.adjustInto(zo_da_time): 2020-06-04T07:51:48.984048+03:00[Africa/Asmara]