Month adjustInto() method in Java

The adjustInto() method of java.time.Month ENUM is an in-built function in Java which takes a Temporal object specifying a date and returns a new Temporal object of the same observable type as the input with the month replaced with this month-of-year.

Method Declaration:

public Temporal adjustInto(Temporal temporal)

Syntax:



Temporal newLocalDate = Month.ANYMONTH.adjustInto(Temporal temporal)

Parameters: This method takes temporal as parameter where:

  • temporal – is the specified date to be adjusted.
  • ANYMONTH – is the specified month to which the date is to be adjusted, e.g., JANUARY, FEBRUARY, etc.
  • newLocalDate – is the modified date.

Return Value: The function returns an adjusted Temporal object which is the date adjusted according to specified Month.

Exceptions:

  • DateTimeException: This method throws this exception if it is not possible to make the adjustment.
  • ArithmeticException: This exception is thrown if numeric overflow occurs.

Below programs illustrate the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.time.*;
import java.time.Month;
import java.time.temporal.Temporal;
  
class DayOfWeekExample {
    public static void main(String[] args)
    {
        // Set a Local Date whose month is found
        LocalDate localDate1
            = LocalDate.of(1947, Month.AUGUST, 15);
  
        // Find the month from the Local Date
        Month monthOfYear1
            = Month.from(localDate1);
  
        // Printing the Local Date
        System.out.println(localDate1
                           + " which is "
                           + monthOfYear1.name());
  
        // Adjust the month to JANUARY from AUGUST
        Temporal localDate2
            = Month.JANUARY
                  .adjustInto(localDate1);
  
        // Find the day from the new Local date
        Month monthOfYear2
            = Month.from(localDate2);
  
        // Printing the new Local Date
        System.out.println(localDate2
                           + " which is "
                           + monthOfYear2.name());
    }
}

chevron_right


Output:

1947-08-15 which is AUGUST
1947-01-15 which is JANUARY

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.time.*;
import java.time.Month;
import java.time.temporal.Temporal;
  
class DayOfWeekExample {
    public static void main(String[] args)
    {
        // Set a Local Date whose month is found
        LocalDate localDate1
            = LocalDate.of(2019, Month.MARCH, 18);
  
        // Find the month from the Local Date
        Month monthOfYear1
            = Month.from(localDate1);
  
        // Printing the Local Date
        System.out.println(localDate1
                           + " which is "
                           + monthOfYear1.name());
  
        // Adjust the month to December from March
        Temporal localDate2
            = Month.DECEMBER
                  .adjustInto(localDate1);
  
        // Find the day from the new Local date
        Month monthOfYear2
            = Month.from(localDate2);
  
        // Printing the new Local Date
        System.out.println(localDate2
                           + " which is "
                           + monthOfYear2.name());
    }
}

chevron_right


Output:

2019-03-18 which is MARCH
2019-12-18 which is DECEMBER

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Month.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.