Java Get Last LocalDate of Current Month

Tags: Java LocalDate YearMonth DateUtil

In this Java tutorial, we learn how to get the last day of current month and return it as a LocalDate object in Java programming language.

How to get last LocalDate of current month in Java

In this first step, we create a new Java class named DateUtil, and implement a new static method named getLastLocalDateOfCurrentMonth(), in this new method we follow these steps to get the last LocalDate object of current month.

  • Step 1: get the YearMonth object of current month using the YearMonth.now() method.
  • Step 2: get the last day of month as LocalDate object using the yearMonth.atEndOfMonth() method.

DateUtil.java

import java.time.LocalDate;
import java.time.YearMonth;

public class DateUtil {

    /**
     * This method to get the last day of current month as a LocalDate object.
     * @return the last LocalDate object of current month.
     */
    public static LocalDate getLastLocalDateOfCurrentMonth() {
        YearMonth yearMonth = YearMonth.now();
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
        return lastDayOfMonth;
    }

}

In the following example Java code, we learn how to use the getLastLocalDateOfCurrentMonth() static method above to get last LocalDate object current month in Java program.

GetLastLocalDateOfCurrentMonthExample.java

import java.time.LocalDate;

public class GetLastLocalDateOfCurrentMonthExample {
    public static void main(String... args) {
        // Get Last LocalDate of Current Month
        LocalDate lastDayOfMonth = DateUtil.getLastLocalDateOfCurrentMonth();

        System.out.println("Today: " + LocalDate.now());
        System.out.println("Last day of current month: " + lastDayOfMonth);
    }
}
The output as below.
Today: 2022-08-20
Last day of current month: 2022-08-31

Happy Coding 😊

Java Get First Date of Current Week

Java Get Last Date of Current Week

Java Get First LocalDate of Current Week

Java Get Last LocalDate of Current Week

Java Get First LocalDate of Current Month

Java Get First LocalDate of Current Year

Java Get Last LocalDate of Current Year

Java Get First Day of Month from Specified Date

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Get First Date of Current Year

Java Get First Date of Current Month

Java Get Same Date in Last Month