Java Data Type How to - Get thanks giving date for any year








Question

We would like to know how to get thanks giving date for any year.

Answer

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.TemporalAdjusters;
//  w  w  w. ja  v  a  2 s. c o  m
public class Main {

  public static void main(String[] args) {
    System.out.println(thanksgiving(2014));
  }

  private static LocalDate thanksgiving(int year) {
    LocalDate thanksGiving = Year.of(year).atMonth(Month.NOVEMBER).atDay(1)
        .with(TemporalAdjusters.lastInMonth(DayOfWeek.WEDNESDAY));
    return thanksGiving;
  }
}

The code above generates the following result.