Java ChronoLocalDate adjustInto(Temporal temporal)

PreviousNext

The adjustInto() method of ChronoLocalDate interface adjusts the specified temporal object to the same date as this object.

Syntax:

public Temporal adjustInto(Temporal temporal)

Parameter: temporal which is the target object to be adjusted, and not specifically null.

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

Example

The following code shows how to use adjustInto() method of ChronoLocalDate in Java:

// Program to illustrate the adjustInto() method

import java.util.*;
import java.time.*;
import java.time.chrono.*;

public class Main {
    public static void main(String[] args) {

        ZonedDateTime date = ZonedDateTime.now();

        // prints the date
        System.out.println(date);

        // Parses the date
        ChronoLocalDate 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);
    }//  w    ww  .   d e   m o   2   s.  c    o m 
}

Result

Another example:

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.*;
import java.time.chrono.*;

public class Main {
    public static void main(String[] args) {
        try {//  w   w  w  .   d e  m o    2s    .c    o  m
            ZonedDateTime date = ZonedDateTime.now();

            // prints the date
            System.out.println(date);

            // Parses the date
            ChronoLocalDate 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);
        }
    }
}

Result

PreviousNext

Related