Java Get Last Date of Current Month

Tags: Java Date DateUtil

In this Java tutorial, we learn how to write Java utility class to get the last day of current month as a Date object in Java programming language.

How to get last day of current month in Java

In this first step, we create a new Java class named DateUtil, and implement new static method named getLastDayOfCurrentMonth() to return Date object represent the last day of current month with the time set to the end of day.

DateUtil.java

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * This method to get the last day of current month and return a Date object.
     * @return the last Date object of current month
     */
    public static Date getLastDayOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
}

In the following example Java code, we learn how to use the above DateUtil class in Java program to get last Date of current month and print out the result.

GetLastDayOfCurrentMonthExample.java

import java.util.Date;

public class GetLastDayOfCurrentMonthExample {
    public static void main(String... args) {
        // Get Last Date of Current Month
        Date lastDate = DateUtil.getLastDayOfCurrentMonth();

        System.out.println("Today: " + new Date());
        System.out.println("Last Date of current month: " + lastDate);
    }
}
The output as below.
Today: Wed Aug 17 21:07:25 ICT 2022
Last Date of current month: Wed Aug 31 23:59:59 ICT 2022

Happy Coding 😊

Java Get First Day of Month from Specified Date

Java Get Yesterday Date

Java Get Tomorrow Date

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Check if Calendar is Week Day or Weekend Day

Java Check if Date is Week Day or Weekend Day

Java Check if Today is Week Day or Weekend Day

Java Convert String to Date

Java Convert Date to Start of Day Time

Java Convert Date to End of Day Time

Java Get First Date of Current Year

Java Get First Date of Current Month

Java Get Same Date in Last Month

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 Last LocalDate of Current Month

Java Get First LocalDate of Current Year

Java Get Last LocalDate of Current Year