Work day query

This example is a custom TemporalQuery that will check if a date is a work day using the standard 5 day Monday through Friday work week. Implementing the TemporalQuery interface with the queryFrom(TemporalAccessor) method allows for a way to extract information from a temporal object. The queryFrom method compares the passed-in date against the DayOfWeek and returns FALSE if the day of the week is SATURDAY or SUNDAY else returning true.

Setup

public class WorkDayQuery implements TemporalQuery<Boolean>{

    /* 
     * 
     * (non-Javadoc)
     * @see java.time.temporal.TemporalQuery#queryFrom(java.time.temporal.TemporalAccessor)
     */
    @Override
    public Boolean queryFrom(TemporalAccessor date) {
        
        int dayOfWeekNumber = date.get(ChronoField.DAY_OF_WEEK);
        DayOfWeek dayOfWeek = DayOfWeek.of(dayOfWeekNumber);
        
        if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
            return Boolean.FALSE; 
        } else {
            return Boolean.TRUE; 
        }
    }
}

Work day

@Test
public void work_day_query () {
    
    LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 02); // Sunday
    
    Boolean workDay = date.query(new WorkDayQuery());
    
    assertFalse(workDay);
}

Use query with lambda

@Test
public void work_day_query_lambda () {
    
    List<LocalDate> randomDays = Lists.newArrayList(
            LocalDate.of(2014, Month.FEBRUARY, 16), // Sunday
            LocalDate.of(2014, Month.APRIL, 19), // Saturday
            LocalDate.of(2014, Month.MAY, 30), // Friday
            LocalDate.of(2014, Month.DECEMBER, 12), // Friday
            LocalDate.of(2014, Month.DECEMBER, 17) // Wednesday
            );
    
    // filter work days from random days 
    List<LocalDate> workDays = randomDays
            .stream()
            .filter(p -> p.query(new WorkDayQuery()))
            .collect(Collectors.toList());
    
    assertEquals(3, workDays.size());
}