Core Java

How to Get Last Day of a Month in Java

In this article, we will delve into the fascinating world of Java date and time manipulation and explore various methods to accomplish a common yet essential task: getting the last day of a month.

1. Introduction

In Java, getting the last day of a month can be essential for various date-related calculations and applications. Fortunately, Java provides several approaches to achieve this. In this guide, we will explore different methods to obtain the last day of a month. We will cover built-in classes from the Java API, as well as custom implementations, to cater to various scenarios.

2. Using java.util.Calendar

The java.util.Calendar class is a standard Java API for working with dates and times. Although it might not be the most modern option, it still provides a straightforward way to obtain the last day of a month. Follow the steps below to achieve this:

2.1. Get Last Day of the Current Month

To get the last day of the current month using java.util.Calendar, use the following code:

import java.util.Calendar;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Get the current date
        Calendar calendar = Calendar.getInstance();

        // Set the date to the first day of the next month
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);

        // Subtract one day to get the last day of the current month
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        // Get the last day of the month
        int lastDay = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("Last day of the current month: " + lastDay);
    }
}
Fig. 1: Get Last Day of a Month in Java Using java.util.Calendar.
Fig. 1: Get Last Day of a Month in Java Using java.util.Calendar.

2.2. Get Last Day of a Specific Month

If you want to find the last day of a specific month, you can set the month and year accordingly before obtaining the last day:

import java.util.Calendar;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Specify the desired month and year
        int year = 2023;
        int month = Calendar.JULY; // Calendar.JULY corresponds to July (0-based index)

        // Get the last day of the specified month and year
        int lastDay = getLastDayOfMonth(year, month);

        System.out.println("Last day of July 2023: " + lastDay);
    }

    private static int getLastDayOfMonth(int year, int month) {
        // Get the last day of the specified month and year
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
}
Fig. 2: Using Calendar.
Fig. 2: Using Calendar.

3. Using java.time.LocalDate

Java 8 introduced the new Date-Time API in the java.time package. This API provides a more modern and intuitive way to work with dates and times. To get the last day of a month using java.time.LocalDate, follow the steps below:

3.1. Get Last Day of the Current Month

To get the last day of the current month using java.time.LocalDate, use the following code:

import java.time.LocalDate;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Get the current date
        LocalDate currentDate = LocalDate.now();

        // Get the last day of the current month
        int lastDay = currentDate.lengthOfMonth();

        System.out.println("Last day of the current month: " + lastDay);
    }
}
Fig. 3: Get Last Day of a Month Using LocalDate.
Fig. 3: Get Last Day of a Month Using LocalDate.

3.2. Get Last Day of a Specific Month

If you want to find the last day of a specific month, you can create a LocalDate object for that month and then obtain the last day:

import java.time.LocalDate;
import java.time.Month;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Specify the desired month and year
        int year = 2023;
        Month month = Month.JULY;

        // Get the last day of the specified month and year
        int lastDay = getLastDayOfMonth(year, month);

        System.out.println("Last day of July 2023: " + lastDay);
    }

    private static int getLastDayOfMonth(int year, Month month) {
        // Get the last day of the specified month and year
        LocalDate date = LocalDate.of(year, month, 1);
        return date.lengthOfMonth();
    }
}
Fig. 4: Using LocalDate.
Fig. 4: Using LocalDate.

4. Using org.apache.commons.lang3.time

If you’re working with an older version of Java without the new Date-Time API or want to use a third-party library for convenience, you can use Apache Commons Lang. Apache Commons Lang is a popular library that provides utility methods for various operations, including date manipulation.

4.1. Get Last Day of the Current Month

To get the last day of the current month using Apache Commons Lang, use the following code:

import org.apache.commons.lang3.time.DateUtils;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Get the last day of the current month
        int lastDay = DateUtils.getLastDayOfMonth();

        System.out.println("Last day of the current month: " + lastDay);
    }
}

4.2. Get Last Day of a Specific Month

To get the last day of a specific month using Apache Commons Lang, follow these steps:

import org.apache.commons.lang3.time.DateUtils;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Specify the desired month and year
        int year = 2023;
        int month = 7; // July (0-based index)

        // Get the last day of the specified month and year
        int lastDay = getLastDayOfMonth(year, month);

        System.out.println("Last day of July 2023: " + lastDay);
    }

    private static int getLastDayOfMonth(int year, int month) {
        // Get the last day of the specified month and year
        int[] lastDayOfMonth = DateUtils.getLastDayOfMonth(year, month);
        return lastDayOfMonth[2];
    }
}

5. Using java.time.YearMonth

In Java 8 and later versions, you can also use the java.time.YearMonth class to find the last day of a month. This class represents a year and month without a specific day or time.

5.1. Get Last Day of the Current Month

To get the last day of the current month using java.time.YearMonth, use the following code:

import java.time.YearMonth;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Get the current year and month
        YearMonth currentYearMonth = YearMonth.now();

        // Get the last day of the current month
        int lastDay = currentYearMonth.lengthOfMonth();

    System.out.println("Last day of the current month: " + lastDay);
    }
}

5.2. Get Last Day of a Specific Month

To get the last day of a specific month using java.time.YearMonth, follow these steps:

import java.time.YearMonth;
import java.time.Month;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Specify the desired month and year
        int year = 2023;
        Month month = Month.JULY;

        // Get the last day of the specified month and year
        int lastDay = getLastDayOfMonth(year, month);

        System.out.println("Last day of July 2023: " + lastDay);
    }

    private static int getLastDayOfMonth(int year, Month month) {
        // Get the last day of the specified month and year
        YearMonth yearMonth = YearMonth.of(year, month);
        return yearMonth.lengthOfMonth();
    }
}

6. Using java.time.temporal.TemporalAdjusters

Java 8 introduced the java.time.temporal.TemporalAdjusters class, which provides various useful methods for adjusting dates. One of these methods is lastDayOfMonth(), which returns a TemporalAdjuster that represents the last day of the month.

6.1. Get Last Day of the Current Month

To get the last day of the current month using java.time.temporal.TemporalAdjusters, use the following code:

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Get the current date
        LocalDate currentDate = LocalDate.now();

        // Get the last day of the current month
        LocalDate lastDay = currentDate.with(TemporalAdjusters.lastDayOfMonth());

        System.out.println("Last day of the current month: " + lastDay.getDayOfMonth());
    }
}
Fig. 5: Using java.time.temporal.TemporalAdjusters.
Fig. 5: Using java.time.temporal.TemporalAdjusters.

6.2. Get Last Day of a Specific Month

To get the last day of a specific month using java.time.temporal.TemporalAdjusters, follow these steps:

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

public class LastDayOfMonthExample {

    public static void main(String[] args) {
        // Specify the desired month and year
        int year = 2023;
        Month month = Month.JULY;

        // Get the last day of the specified month and year
        LocalDate lastDay = getLastDayOfMonth(year, month);

        System.out.println("Last day of July 2023: " + lastDay.getDayOfMonth());
    }

    private static LocalDate getLastDayOfMonth(int year, Month month) {
        // Get the last day of the specified month and year
        LocalDate date = LocalDate.of(year, month, 1);
        return date.with(TemporalAdjusters.lastDayOfMonth());
    }
}
Fig. 6: Using TemporalAdjusters.
Fig. 6: Using TemporalAdjusters.

7. Conclusion

In this guide, we explored different methods to get the last day of a month in Java. We covered various approaches, including using java.util.Calendar, java.time.LocalDate, Apache Commons Lang, and java.time.YearMonth. We also learned about the java.time.temporal.TemporalAdjusters class, which provides a convenient way to obtain the last day of the month. Choose the method that best fits your requirements and code with confidence knowing you can easily get the last day of any month in Java.

8. Download the Source Code

This was an example of How to Get the Last Day of a Month in Java using various methods!

Download
You can download the full source code of this example here: How to Get Last Day of a Month in Java

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button