LocalTime adjustInto() method in Java with Examples

The adjustInto() method of LocalTime class is used to adjusts the specified temporal object to have the same time as this LocatTime 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalTime.adjustInto() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Object
        LocalTime local
            = LocalTime.parse("09:32:42");
  
        // create Zone Time
        ZonedDateTime zonetime = ZonedDateTime.now();
  
        // print Zone Time
        System.out.println("ZonedDateTime"
                           + " before adjustInto():"
                           + zonetime.toString());
  
        // apply adjustInto()
        zonetime = (ZonedDateTime)local
                       .adjustInto(zonetime);
  
        // print Zone Time
        System.out.println("ZonedDateTime"
                           + " after adjustInto():"
                           + zonetime.toString());
    }
}

chevron_right


Output:

ZonedDateTime before adjustInto():2018-12-03T06:30:31.142Z[Etc/UTC]
ZonedDateTime after adjustInto():2018-12-03T09:32:42Z[Etc/UTC]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalTime.adjustInto() method
  
import java.time.*;
import java.time.temporal.Temporal;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Object
        LocalTime local = LocalTime.parse("19:52:43");
  
        // create a Temporal object
        // which is equal to OffsetDateTime object
        OffsetDateTime passTemporal
            = OffsetDateTime.now();
  
        // print passed Value
        System.out.println("Before adjustInto() OffsetDateTime: "
                           + passTemporal);
  
        // apply adjustInto method
        // to adjust OffsetDateTime Temporal
        Temporal returnTemporal
            = local.adjustInto(passTemporal);
  
        // print results
        System.out.println("After adjustInto() OffsetDateTime: "
                           + (OffsetDateTime)returnTemporal);
    }
}

chevron_right


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)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.