Get first and last day of month using threeten, LocalDate
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, working with dates and times has become more structured and intuitive with the introduction of the java.time package in Java 8, also known as JSR-310 or "ThreeTen" (named after its Java Specification Request number). This package provides the LocalDate class, which is central to handling date manipulations without time-zone considerations. In this article, we will delve into how you can obtain the first and last days of a month using LocalDate.
Understanding LocalDate
LocalDate is a class that represents a date without time-zone and time information. It can be used for, among other things, budgeting monthly tasks, timesheets, and creating simple calendar applications. Central to our discussion today will be its ability to elegantly manipulate date information.
Essential Methods
Here are some essential methods provided by the LocalDate class that we'll use and are worth familiarizing:
now(): Obtains the current date from the system clock.of(): Creates an instance ofLocalDatefor a specific date.withDayOfMonth(int dayOfMonth): Returns a copy of theLocalDatewith the specified day of the month.lengthOfMonth(): Returns the length of the month represented by theLocalDate, which can change depending on leap years.
Getting the First and Last Day of the Month
The primary task is to determine the first and last days of a given month using LocalDate. We accomplish this task utilizing fluid methods available in the LocalDate API.
Example - Current Month
Below is a step-by-step implementation to find the first and last day of the current month:
Example - Specific Month and Year
Sometimes, you may want to find the first and last days of a particular month-year combination:
Special Considerations
Leap Years
The LocalDate class automatically takes leap years into account. If you specify February in a leap year (like 2024), lengthOfMonth() will correctly return 29.
Validating Dates
It ensures that inputs like month and day combinations are valid. Trying to create an invalid date will throw a DateTimeException.
Table of Key Methods
| Method | Description |
now() | Obtains current date from the system clock. |
of(year, month, dayOfMonth) | Creates an instance of LocalDate for a specific date. |
withDayOfMonth(day) | Replaces the day of the month in the LocalDate instance to a specified value. |
lengthOfMonth() | Returns the number of days in the month. Takes leap years into account when applicable. |
Conclusion
The java.time.LocalDate class offers a simple yet powerful way to handle dates in Java. By leveraging its methods, obtaining the first and last days of a month—whether it's the current month or a specified month—is not only straightforward but also reliable. This approach abstracts much of the traditional complexity encountered with date manipulation, making it a preferred choice for contemporary Java applications.

