TemporalAdjusters class

The TemporalAdjusters class, part of java.time.temporal, provides common date adjustment operations as static methods that return TemporalAdjuster instances. These are used with date-time objects to manipulate or align them to significant calendar dates—like the first day of the month, next Monday, etc.

Commonly Used Methods

Simple Program

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

public class TemporalAdjusterExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        LocalDate firstDay = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate nextMonday = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfYear());

        System.out.println("Today: " + today);
        System.out.println("First Day of Month: " + firstDay);
        System.out.println("Next Monday: " + nextMonday);
        System.out.println("Last Day of Year: " + lastDay);
    }
}

Problem Statement

LotusJavaPrince and Mahesh are building a leave management system for employees. The system must:

  • Automatically find the next Friday for optional leave.
  • Identify the last day of the month for payroll processing.
  • Find the first Monday of the next month for scheduling training.
  • Ensure all adjustments are dynamic based on today’s date.
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.temporal.TemporalAdjusters;

public class LeaveSystem {
    public static void main(String[] args) {
        System.out.println("Leave Management System by LotusJavaPrince & Mahesh");

        LocalDate today = LocalDate.now();

        LocalDate nextFriday = today.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        LocalDate payrollDate = today.with(TemporalAdjusters.lastDayOfMonth());
        LocalDate firstMondayNextMonth = today
                .with(TemporalAdjusters.firstDayOfNextMonth())
                .with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));

        System.out.println("Today's Date: " + today);
        System.out.println("Next Optional Leave (Friday): " + nextFriday);
        System.out.println("Payroll Processing Date: " + payrollDate);
        System.out.println("First Training Monday of Next Month: " + firstMondayNextMonth);
    }
}
/*
Leave Management System by LotusJavaPrince & Mahesh
Today's Date: 2025-05-22
Next Optional Leave (Friday): 2025-05-23
Payroll Processing Date: 2025-05-31
First Training Monday of Next Month: 2025-06-02
*/

The TemporalAdjusters class is a powerful utility for manipulating and navigating calendar dates based on logical rules. It is ideal for:

  • Business rule-based date computation.
  • Employee scheduling, payroll systems, and event planning.
  • Reducing boilerplate logic for date calculations.
Scroll to Top