Close

Java Date Time - How to find date of nth week day in a month?

Java Date Time Java 

This example shows how to find the date of a particular occurrence of week-day in a month. For example how to find 3rd Monday's date in the month of January for a given year?

package com.logicbig.example;

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

public class DateAtNWeekDay {

public static void main (String[] args) {
//from LocalDate
LocalDate d = LocalDate.of(2017, Month.JANUARY, 10);
LocalDate d2 = d.with(
TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.MONDAY));
System.out.println(d2);

//from YearMonth
YearMonth ym = YearMonth.of(2017, Month.JANUARY);
LocalDate d3 = ym.atDay(1)
.with(TemporalAdjusters.dayOfWeekInMonth(
3, DayOfWeek.MONDAY));
System.out.println(d3);
}
}

Output

2017-01-16
2017-01-16




See Also