Java Get Last Date of Specified Month

Tags: Java Date DateUtil

In this Java tutorial, we learn how to write a Java utility class to get the last day in month of a specified month and year then return it as a Date object in Java programming language.

How to get last day of specified month in Java

In this step, we create a new Java class named DateUtil, and implement a new static method named getLastDateOfMonth(int year, int month) which get the value of month and year then return the last day of month as a Date object with time value set to end of day.

DateUtil.java

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

public class DateUtil {

    /**
     * This method to return the last day of a specified month / year as a Date object
     * @param year the year value
     * @param month the month value
     * @return the Date object of last day of month
     */
    public static Date getLastDateOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        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 write Java program to get last day of specified month February 2022 and return the Date object.

LastDateOfMonthExample.java

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

public class LastDateOfMonthExample {
    public static void main(String... args) {
        int year = 2023;
        int month = Calendar.FEBRUARY;

        //  Get Last Date of Specified Month
        Date lastDateOfMonth = DateUtil.getLastDateOfMonth(year, month);

        System.out.println("Last day of month: " + lastDateOfMonth);
    }
}
The output as below.
Last day of month: Tue Feb 28 23:59:59 ICT 2023

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 Month

Java Get Last Date of Current Year

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