Thursday, July 4, 2024
HomeLanguagesJavaMonth adjustInto() method in Java

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:




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());
    }
}


Output:

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

Program 2:




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());
    }
}


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-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments