Next holiday adjuster

Example shows how to find the next holiday using a custom Java 8 adjuster. The TemporalAdjuster interface, in the java.time.temporal package, adjustInto method accepts a temporal value and returns an adjusted value. The adjustInto method will evaluate the passed-in date to determine the next holiday.

Setup

public class NextHoliday implements TemporalAdjuster {

    static List<MonthDay> COMPANY_HOLIDAYS = Lists.newArrayList(
        MonthDay.of(Month.JANUARY, 1), // New Years Day   January 1
        MonthDay.of(Month.JANUARY, 20), // Martin Luther King, Jr. Day    January 20
        MonthDay.of(Month.APRIL, 18), // Good Friday  April 18
        MonthDay.of(Month.MAY, 26), // Memorial Day   May 26
        MonthDay.of(Month.JULY, 4), // Independence Day   July 4**
        MonthDay.of(Month.SEPTEMBER, 1), // Labor Day September 1
        MonthDay.of(Month.NOVEMBER, 27), // Thanksgiving Day  November 27*
        MonthDay.of(Month.NOVEMBER, 28), // Day after thanksgiving November 28*
        MonthDay.of(Month.DECEMBER, 25), // Christmas December 25***
        MonthDay.of(Month.DECEMBER, 26) // Day after xmas December 26***
    );

    @Override
    public Temporal adjustInto(Temporal temporal) {

        MonthDay currentMonthDay = MonthDay.from(temporal);
        int year = temporal.get(ChronoField.YEAR);
        
        for (MonthDay element : COMPANY_HOLIDAYS) {
            if (currentMonthDay.isBefore(element)) {
                return element.atYear(year);
            }
        }
        
        // if it hasn't been returned, then return the first element
        return COMPANY_HOLIDAYS.get(0).atYear(year + 1);
    }
}

Find next holiday

@Test
public void next_holiday () {
    
    LocalDate date = LocalDate.of(2009, Month.DECEMBER, 15);
    
    LocalDate nextHoliday = date.with(new NextHoliday());

    assertEquals(
            LocalDate.of(2009, Month.DECEMBER, 25), 
            nextHoliday);
}

Find holiday next year

@Test
public void next_holiday_next_year () {
    
    LocalDate date = LocalDate.of(2010, Month.DECEMBER, 26);
    
    LocalDate nextHoliday = date.with(new NextHoliday());

    assertEquals(
            LocalDate.of(2011, Month.JANUARY, 1), 
            nextHoliday);
}