Java program to adjust LocalDate to last day of month with TemporalAdjusters class



In this article, we will learn the LocalDate, Month, and TemporalAdjusters classes from java.time package. These classes are essential for handling date and time operations in Java.

LocalDate represents a date without time, Month is an enumeration of the twelve months, and TemporalAdjusters provides utility methods for common date adjustments. We will learn how to set a specific date and adjust it to find the first and last days of the month and the first day of the next month.

Steps to adjust LocalDate to last day of month with TemporalAdjusters class

Following are the steps to adjust LocalDate to last day of month with TemporalAdjusters class ?

  • Import necessary classes.
  • Set a specific date using the LocalDate.of() method.
  • Print the current date and month.
  • Adjust the date to the first day of the month.
  • Adjust the date to the last day of the month.
  • Adjust the date to the first day of the next month.
  • Print the adjusted dates.

Java program to adjust LocalDate to last day of month with TemporalAdjusters class

Here is the Java program to adjust a LocalDate to the last day of the month using the TemporalAdjusters class ?

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
public class Demo {
   public static void main(String[] args) {
      LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15);
      System.out.println("Current Date = "+localDate);
      System.out.println("Current Month = "+localDate.getMonth());
      LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth());
      System.out.println("First day of month = "+day);
      day = localDate.with(TemporalAdjusters.lastDayOfMonth());
      System.out.println("Last day of month = "+day);
      day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
      System.out.println("First day of next month = "+day);
   }
}

Output

Current Date = 2019-06-15
Current Month = JUNE
First day of month = 2019-06-01
Last day of month = 2019-06-30
First day of next month = 2019-07-01

Code Explanation

The above program starts by importing LocalDate, Month, and TemporalAdjusters from the java.time package. We initialize a LocalDate instance using the LocalDate.of() method and print the current date and month.

LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15)

Now, adjust LocalDate to the previous Day of the month;

LocalDate day = localDate.with(TemporalAdjusters.lastDayOfMonth());

The TemporalAdjusters class is then used to adjust this date. The TemporalAdjusters.firstDayOfMonth() method sets the date to the first day of the month, while TemporalAdjusters.lastDayOfMonth() sets it to the last day. Finally, TemporalAdjusters.firstDayOfNextMonth() adjusts the date to the first day of the following month. Each adjusted date is printed to show the results of these operations.

Updated on: 2024-08-08T17:38:19+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements