Open In App
Related Articles

LocalDate adjustInto() method in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Syntax:

public 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 LocalDate in Java:

Program 1:




// Program to illustrate the adjustInto() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
  
        ZonedDateTime date = ZonedDateTime.now();
  
        // prints the date
        System.out.println(date);
  
        // Parses the date
        LocalDate date1 = LocalDate.parse("2015-01-31");
  
        // Uses the function to adjust the date
        date = (ZonedDateTime)date1.adjustInto(date);
  
        // Prints the adjusted date
        System.out.println(date);
    }
}


Output:

2018-11-28T05:36:08.205Z[Etc/UTC]
2015-01-31T05:36:08.205Z[Etc/UTC]

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




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


Output:

2018-11-28T05:36:11.014Z[Etc/UTC]
java.time.format.DateTimeParseException: 
Text '2015-02-31' could not be parsed: Invalid date 'FEBRUARY 31'

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#adjustInto(java.time.temporal.Temporal)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Nov, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials