90 Day Weekend Adjuster

This examples will show how to find 90 days business days from a date 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 NinetyDayWeekendAdjuster will evaluate the passed in date and return 90 days excluding weekends by creating a date 90 days in the future. By calling date.query and passing a query that will check if a date is a weekend day, either Saturday or Sunday, if so adding 1 day into the future.

Setup

public class NinetyDayWeekendAdjuster implements TemporalAdjuster {

    @Override
    public Temporal adjustInto(Temporal temporal) {

        LocalDateTime currentDateTime = LocalDateTime.from(temporal);

        LocalDateTime futureDate = LocalDateTime.from(temporal).plus(90,
                ChronoUnit.DAYS);

        for (LocalDateTime startDate = currentDateTime; startDate
                .isBefore(futureDate); startDate = startDate.plusDays(1)) {

            if (startDate.query(new WeekendQuery())) {
                futureDate = futureDate.plusDays(1);
            }
        }
        return futureDate;
    }
}

90 business days

@Test
public void test() {

    LocalDateTime currentDateTime = LocalDateTime.of(2014, Month.APRIL, 30,
            0, 0);

    LocalDateTime adjustedDate = currentDateTime
            .with(new NinetyDayWeekendAdjuster());

    assertEquals(LocalDateTime.of(2014, Month.SEPTEMBER, 3, 0, 0),
            adjustedDate);

}