Example usage for java.time.temporal Temporal get

List of usage examples for java.time.temporal Temporal get

Introduction

In this page you can find the example usage for java.time.temporal Temporal get.

Prototype

default int get(TemporalField field) 

Source Link

Document

Gets the value of the specified field as an int .

Usage

From source file:Main.java

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()));
        }//w w  w  . java  2 s  .  c o m

        @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
}

From source file:msi.gama.util.GamaDate.java

public GamaDate(final IScope scope, final Temporal d) {
    final ZoneId zone;
    if (d instanceof ChronoZonedDateTime) {
        zone = ZonedDateTime.from(d).getZone();
    } else if (d.isSupported(ChronoField.OFFSET_SECONDS)) {
        zone = ZoneId.ofOffset("", ZoneOffset.ofTotalSeconds(d.get(ChronoField.OFFSET_SECONDS)));
    } else {//from  w w w. j  av  a2  s .  c  om
        zone = GamaDateType.DEFAULT_ZONE;
    }
    if (!d.isSupported(MINUTE_OF_HOUR)) {
        internal = ZonedDateTime.of(LocalDate.from(d), LocalTime.of(0, 0), zone);
    } else if (!d.isSupported(DAY_OF_MONTH)) {
        internal = ZonedDateTime.of(LocalDate.from(
                scope == null ? Dates.DATES_STARTING_DATE.getValue() : scope.getSimulation().getStartingDate()),
                LocalTime.from(d), zone);
    } else {
        internal = d;
    }
}