Java Data Type How to - Get days before Christmas with TemporalQuery








Question

We would like to know how to get days before Christmas with TemporalQuery.

Answer

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalQuery;
/*  w ww .j  av  a  2  s .  com*/
public class Main {

  public static void main(String[] args) {
    TemporalQuery<Integer> daysBeforeChristmas = new TemporalQuery<Integer>() {

      public int daysTilChristmas(int acc, Temporal temporal) {
        int month = temporal.get(ChronoField.MONTH_OF_YEAR);
        int day = temporal.get(ChronoField.DAY_OF_MONTH);
        int max = temporal.with(TemporalAdjusters.lastDayOfMonth()).get(
            ChronoField.DAY_OF_MONTH);
        if (month == 12 && day <= 25)
          return acc + (25 - day);
        return daysTilChristmas(acc + (max - day + 1),
            temporal.with(TemporalAdjusters.firstDayOfNextMonth()));
      }

      @Override
      public Integer queryFrom(TemporalAccessor temporal) {
        if (!(temporal instanceof Temporal))
          throw new RuntimeException(
              "Temporal accessor must be of type Temporal");
        return daysTilChristmas(0, (Temporal) temporal);
      }
    };

    System.out.println(LocalDate.of(2013, 12, 26).query(daysBeforeChristmas)); // 364
    System.out.println(LocalDate.of(2013, 12, 23).query(daysBeforeChristmas)); // 2
    System.out.println(LocalDate.of(2013, 12, 25).query(daysBeforeChristmas)); // 0
    System.out.println(ZonedDateTime.of(2013, 12, 1, 11, 0, 13, 938282,
        ZoneId.of("America/Los_Angeles")).query(daysBeforeChristmas)); // 24
  }
}

The code above generates the following result.