Explain Codes LogoExplain Codes Logo

Get first and last day of month using threeten, LocalDate

java
date-manipulation
temporal-adjusters
localdate
Nikita BarsukovbyNikita Barsukov·Dec 9, 2024
TLDR

Fetch the first and last day of a month using LocalDate and YearMonth:

  • localDate.withDayOfMonth(1): Retrieves the month's initial day.
  • yearMonth.atEndOfMonth(): Procures the month's final day.
import org.threeten.bp.LocalDate; import org.threeten.bp.YearMonth; LocalDate today = LocalDate.now(); LocalDate firstDay = today.withDayOfMonth(1); // Not an astrology sign, but rather the first day LocalDate lastDay = YearMonth.from(today).atEndOfMonth(); // The last dance with the month

These expressions provide the current month's start and end dates perfect for any LocalDate manipulation.

Digging deeper: A touch of TemporalAdjusters

Enhance date manipulations with TemporalAdjusters:

import org.threeten.bp.temporal.TemporalAdjusters; LocalDate firstDayAdjusted = today.with(TemporalAdjusters.firstDayOfMonth()); // Favouring TemporalAdjusters since it's the first in the month LocalDate lastDayAdjusted = today.with(TemporalAdjusters.lastDayOfMonth()); // Last call! Courtesy of TemporalAdjusters

Improve readability and maintainability with static imports:

import static org.threeten.bp.temporal.TemporalAdjusters.firstDayOfMonth; import static org.threeten.bp.temporal.TemporalAdjusters.lastDayOfMonth; LocalDate firstDayClean = today.with(firstDayOfMonth()); // Cleaned up nicely for the first date LocalDate lastDayClean = today.with(lastDayOfMonth()); // Out with a bang using lastDayOfMonth

Leap years got you leaping? These methods got you covered for February's extra day.

For a change, consider using YearMonth:

YearMonth yearMonthInstance = YearMonth.from(today); LocalDate firstDayYearMonth = yearMonthInstance.atDay(1); // Nothing tricky - it's day one LocalDate lastDayYearMonth = yearMonthInstance.atEndOfMonth(); // YearMonth sends regards for the last day

YearMonth handles monthly cycles like a charm, particularly in business logic.

Crafting the last day from known lengths

Knowing the month's length? plusDays() has your back:

LocalDate lastDayKnownLength = firstDay.plusDays(monthLength - 1); // The last crusader from the known lengths kingdom

Remember, we start at day one, thus the reminder to subtract one from the known monthLength.

Designing date manually

When precision is key, and you need to manually set a specific date, not related to today:

LocalDate firstDaySpecific = LocalDate.of(year, month, 1); // The wizard has constructed the first day LocalDate lastDaySpecific = firstDaySpecific.withDayOfMonth( firstDaySpecific.lengthOfMonth() // A specific wizard's tool to get the very last day );

Always cross-check date operations with business rules for avoiding surprises.

Weighing benefits: LocalDate, TemporalAdjusters and YearMonth

Clarity above all

For understandable and clean code, methods like .with(firstDayOfMonth()) offer obvious insight about the outcome.

Optimized performance

In an environment that demands high performance, LocalDate with less method calls like .withDayOfMonth() could save the day.

Maintainability bonus

Utilizing static imports from TemporalAdjusters or YearMonth eases code maintenance and results in more contained modules.