LocalTime adjustInto() method in Java with Examples
Last Updated :
05 Apr, 2023
The adjustInto() method of LocalTime class is used to adjusts the specified temporal object to have the same time as this LocalTime Object. Syntax:
public Temporal adjustInto(Temporal temporal)
Parameters: This method accepts a single parameter temporal which is the target object to be adjusted, and not specifically null. Return value: This method returns the adjusted temporal object. Exception: The function 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:
Java
import java.time.*;
public class GFG {
public static void main(String[] args)
{
LocalTime local
= LocalTime.parse(" 09 : 32 : 42 ");
ZonedDateTime zonetime = ZonedDateTime.now();
System.out.println("ZonedDateTime"
+ " before adjustInto():"
+ zonetime.toString());
zonetime = (ZonedDateTime)local
.adjustInto(zonetime);
System.out.println("ZonedDateTime"
+ " after adjustInto():"
+ zonetime.toString());
}
}
|
Output:
ZonedDateTime before adjustInto():2018-12-03T06:30:31.142Z[Etc/UTC]
ZonedDateTime after adjustInto():2018-12-03T09:32:42Z[Etc/UTC]
Program 2:
Java
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
LocalTime local = LocalTime.parse(" 19 : 52 : 43 ");
OffsetDateTime passTemporal
= OffsetDateTime.now();
System.out.println("Before adjustInto() OffsetDateTime: "
+ passTemporal);
Temporal returnTemporal
= local.adjustInto(passTemporal);
System.out.println("After adjustInto() OffsetDateTime: "
+ (OffsetDateTime)returnTemporal);
}
}
|
Output:
Before adjustInto() OffsetDateTime: 2018-12-03T06:30:33.927Z
After adjustInto() OffsetDateTime: 2018-12-03T19:52:43Z
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#adjustInto(java.time.temporal.Temporal)
Share your thoughts in the comments
Please Login to comment...