ChronoLocalDateTime adjustInto() method in Java with Examples

The adjustInto() method of ChronoLocalDateTime interface in Java is used to adjusts the specified temporal object to have the same date as this object.

Syntax:

default Temporal adjustInto(Temporal temporal)

Parameter: This method accepts a single parameter temporal which is the target object to be adjusted, and not specifically null.



Return Value: It returns the adjusted object, not null.

Exceptions: The function throws two exceptions as described below:

  1. DateTimeException: the program throws this if it is unable to make the adjustment.
  2. ArithmeticException: the program throws this if there is a numeric overflow.

Below programs illustrate the adjustInto() method of ChronoLocalDateTime in Java:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the adjustInto() method
  
import java.util.*;
import java.time.*;
import java.time.chrono.*;
  
public class GfG {
    public static void main(String[] args)
    {
  
        LocalDateTime date
            = LocalDateTime
                  .parse("2018-12-06T19:21:12");
  
        // prints the date
        System.out.println(date);
  
        // Parses the date
        ChronoLocalDateTime date1
            = LocalDateTime.now();
  
        // Uses the function to adjust the date
        date = (LocalDateTime)date1.adjustInto(date);
  
        // Prints the adjusted date
        System.out.println(date);
    }
}

chevron_right


Output:

2018-12-06T19:21:12
2019-05-14T09:39:37.953

Program 2: To illustrate Exception. The below program throws an exception as February is of 28 days and not 31 days.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the adjustInto() method
// Exception Program
  
import java.util.*;
import java.time.*;
import java.time.chrono.*;
  
public class GfG {
    public static void main(String[] args)
    {
        try {
  
            LocalDateTime date
                = LocalDateTime
                      .parse("2018-12-06T19:21:12");
  
            // prints the date
            System.out.println(date);
  
            // Parses the date
            ChronoLocalDateTime date1
                = LocalDateTime.parse("2015-02-31");
  
            // Uses the function to adjust the date
            date = (LocalDateTime)date1.adjustInto(date);
  
            // Prints the adjusted date
            System.out.println(date);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

2018-12-06T19:21:12
java.time.format.DateTimeParseException:
 Text '2015-02-31' could not be parsed at index 10

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#adjustInto-java.time.temporal.Temporal-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.