OffsetTime adjustInto() method in Java with examples

The adjustInto() method of OffsetDateTime class in Java adjusts the specified temporal object to have the same 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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the adjustInto() method
  
import java.time.OffsetTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Current time
        ZonedDateTime date = ZonedDateTime.now();
  
        // Prints the current time
        System.out.println("Current date:" + date);
  
        // Parsed the date
        OffsetTime date1 = OffsetTime.parse("14:30:30+05:00");
  
        // Adjusts
        date = (ZonedDateTime)date1.adjustInto(date);
        System.out.println("Adjusted date:" + date);
    }
}

chevron_right


Output:

Current date:2018-12-11T10:01:37.877Z[Etc/UTC]
Adjusted date:2018-12-11T14:30:30Z[Etc/UTC]

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the adjustInto()
// method exceptions
  
import java.time.OffsetTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
        try {
            // Current time
            // Current time
            ZonedDateTime date = ZonedDateTime.now();
  
            // Prints the current time
            System.out.println("Current date:" + date);
  
            // Parsed the date
            OffsetTime date1 = OffsetTime.parse("25:30:30+05:00");
  
            // Adjusts
            date = (ZonedDateTime)date1.adjustInto(date);
            System.out.println("Adjusted date:" + date);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

Current date:2018-12-11T10:01:42.520Z[Etc/UTC]
java.time.format.DateTimeParseException: Text '25:30:30+05:00' could not be parsed: Invalid value for HourOfDay (valid values 0 - 23): 25

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#adjustInto-java.time.temporal.Temporal-



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.



Improved By : Akanksha_Rai