Java LocalDate Class | adjustInto() Method with Example

LocalDate Class adjustInto() method: Here, we are going to learn about the adjustInto() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on May 29, 2020

LocalDate Class adjustInto() method

  • adjustInto() method is available in java.time package.
  • adjustInto() method is used to adjust this LocalDate object into the given Temporal object or in other words we can say the given object holds the same date as this LocalDate object date.
  • adjustInto() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • adjustInto() method may throw an exception at the time of adjusting the Date.
    • DateTimeException: This exception may throw when the given parameter couldn't adjust this LocalDate.
    • ArithmeticException: This exception may throw when the calculated result exceeds the length of this Date.

Syntax:

    public Temporal adjustInto(Temporal temp);

Parameter(s):

  • Temporal temp – represents the temporal object to adjust this LocalDate.

Return value:

The return type of this method is Temporal, it returns the Temporal object that holds the value adjusted this object into the given object.

Example:

// Java program to demonstrate the example 
// of adjustInto(Temporal temp) method 
// of LocalDate

import java.time.*;

public class AdjustIntoOfLocalDate {
    public static void main(String args[]) {
        // LocalDateiates two LocalDate
        // and a ZonedDateTime object
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.now();
        ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-08-08T20:25:30.213+03:00[Africa/Asmara]");

        // Display l_da1,l_da2
        // and zo_da_time
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);
        System.out.println();
        System.out.println("ZonedDateTime zo_da_time: ");
        System.out.println("zo_da_time: " + zo_da_time);

        System.out.println();

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this LocalDate(l_da1) into the
        // given object ZonedDateTime(zo_da_time)
        zo_da_time = (ZonedDateTime) l_da1.adjustInto(zo_da_time);

        // Display updated zo_da_time
        System.out.println("l_da1.adjustInto(zo_da_time): " + zo_da_time);

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this LocalDate(l_da2) into the
        // given object ZonedDateTime(zo_da_time)
        zo_da_time = (ZonedDateTime) l_da2.adjustInto(zo_da_time);

        // Display updated zo_da_time
        System.out.println("l_da2.adjustInto(zo_da_time): " + zo_da_time);
    }
}

Output

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-04
l_da2: 2020-05-29

ZonedDateTime zo_da_time: 
zo_da_time: 2008-08-08T20:25:30.213+03:00[Africa/Asmara]

l_da1.adjustInto(zo_da_time): 2007-04-04T20:25:30.213+03:00[Africa/Asmara]
l_da2.adjustInto(zo_da_time): 2020-05-29T20:25:30.213+03:00[Africa/Asmara]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.