Java.Java8.How to get second Saturday of current month using Date Time API?

✅ Step-by-step Approach

  1. Get the first day of the current month
  2. Use TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SATURDAY) to find the 2nd Saturday

✅ Full Example:

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

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

        LocalDate secondSaturday = today
            .withDayOfMonth(1) // first day of current month
            .with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SATURDAY)); // 2nd Saturday

        System.out.println("Today:            " + today);
        System.out.println("Second Saturday:  " + secondSaturday);
    }
}

🔹 Output Example (assuming today is April 15, 2025):

Today:            2025-04-15
Second Saturday:  2025-04-12

🧠 Tip:

If you want the first Monday, third Sunday, or last Friday, just change:

TemporalAdjusters.dayOfWeekInMonth(N, DayOfWeek.X)

Or use:

TemporalAdjusters.lastInMonth(DayOfWeek.X)
This entry was posted in Без рубрики. Bookmark the permalink.