@Override public <R> R query( TemporalQuery<R> query ) { return temporal().query( query ); }
final LocalDate expectedDate = ((Temporal) expectedValue).query( TemporalQueries.localDate() ); final LocalTime expectedTime = ((Temporal) expectedValue).query( TemporalQueries.localTime() ); final ZoneId expectedZoneId = ((Temporal) expectedValue).query( TemporalQueries.zone() ); LocalDate actualDate = ((Temporal) actualValue).query( TemporalQueries.localDate() ); LocalTime actualTime = ((Temporal) actualValue).query( TemporalQueries.localTime() ); ZoneId actualZoneId = ((Temporal) actualValue).query( TemporalQueries.zone() );
@Override public <R> R query( TemporalQuery<R> query ) { return temporal().query( query ); }
/** * Obtains an instance consisting of the amount of time between two temporals. * <p> * The start is included, but the end is not. * The result of this method can be negative if the end is before the start. * <p> * The calculation examines the temporals and extracts {@link LocalDate} and {@link LocalTime}. * If the time is missing, it will be defaulted to midnight. * If one date is missing, it will be defaulted to the other date. * It then finds the amount of time between the two dates and between the two times. * * @param startInclusive the start, inclusive, not null * @param endExclusive the end, exclusive, not null * @return the number of days between this date and the end date, not null */ public static PeriodDuration between(Temporal startInclusive, Temporal endExclusive) { LocalDate startDate = startInclusive.query(TemporalQueries.localDate()); LocalDate endDate = endExclusive.query(TemporalQueries.localDate()); Period period = Period.ZERO; if (startDate != null && endDate != null) { period = Period.between(startDate, endDate); } LocalTime startTime = startInclusive.query(TemporalQueries.localTime()); LocalTime endTime = endExclusive.query(TemporalQueries.localTime()); startTime = startTime != null ? startTime : LocalTime.MIDNIGHT; endTime = endTime != null ? endTime : LocalTime.MIDNIGHT; Duration duration = Duration.between(startTime, endTime); return PeriodDuration.of(period, duration); }
@Override protected boolean matchesSafely(final T actual, final Description mismatchDesc) { if (!this.adapter.asTemporal(actual, zone).query(localDate()).isLeapYear()) { mismatchDesc.appendText("the date " + this.formatter.describe(actual) + " is not a leap year"); return false; } else { return true; } }
/** * Queries this year-quarter using the specified query. * <p> * This queries this year-quarter using the specified query strategy object. * The {@code TemporalQuery} object defines the logic to be used to * obtain the result. Read the documentation of the query to understand * what the result of this method will be. * <p> * The result of this method is obtained by invoking the * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the * specified query passing {@code this} as the argument. * * @param <R> the type of the result * @param query the query to invoke, not null * @return the query result, null may be returned (defined by the query) * @throws DateTimeException if unable to query (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ @SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { if (query == TemporalQueries.chronology()) { return (R) IsoChronology.INSTANCE; } else if (query == TemporalQueries.precision()) { return (R) QUARTER_YEARS; } return Temporal.super.query(query); }
@Override protected T valueNonNull(JsonNode node) { Temporal temporal = ISODateParser.parser().fromString(node.asText()); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(0); ZoneId zone = temporal.query(TemporalQueries.zone()); if (zone != null) { calendar.setTimeZone(TimeZone.getTimeZone(zone)); } if (temporal.isSupported(ChronoField.YEAR)) { int year = temporal.get(ChronoField.YEAR); int monthOfYear = temporal.get(ChronoField.MONTH_OF_YEAR); int dayOfMonth = temporal.get(ChronoField.DAY_OF_MONTH); calendar.set(year, --monthOfYear, dayOfMonth); } if (temporal.isSupported(ChronoField.HOUR_OF_DAY)) { int hours = temporal.get(ChronoField.HOUR_OF_DAY); int minutes = temporal.get(ChronoField.MINUTE_OF_HOUR); int seconds = temporal.get(ChronoField.SECOND_OF_MINUTE); calendar.set(Calendar.HOUR_OF_DAY, hours); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.SECOND, seconds); } if (temporal.isSupported(ChronoField.MILLI_OF_SECOND)) { int millis = temporal.get(ChronoField.MILLI_OF_SECOND); calendar.setTimeInMillis(calendar.getTimeInMillis() + millis); } return normalizer.apply(calendar.getTime()); }
@Override protected T valueNonNull(JsonNode node) { Temporal temporal = ISODateParser.parser().fromString(node.asText()); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(0); ZoneId zone = temporal.query(TemporalQueries.zone()); if (zone != null) { calendar.setTimeZone(TimeZone.getTimeZone(zone)); } if (temporal.isSupported(ChronoField.YEAR)) { int year = temporal.get(ChronoField.YEAR); int monthOfYear = temporal.get(ChronoField.MONTH_OF_YEAR); int dayOfMonth = temporal.get(ChronoField.DAY_OF_MONTH); calendar.set(year, --monthOfYear, dayOfMonth); } if (temporal.isSupported(ChronoField.HOUR_OF_DAY)) { int hours = temporal.get(ChronoField.HOUR_OF_DAY); int minutes = temporal.get(ChronoField.MINUTE_OF_HOUR); int seconds = temporal.get(ChronoField.SECOND_OF_MINUTE); calendar.set(Calendar.HOUR_OF_DAY, hours); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.SECOND, seconds); } if (temporal.isSupported(ChronoField.MILLI_OF_SECOND)) { int millis = temporal.get(ChronoField.MILLI_OF_SECOND); calendar.setTimeInMillis(calendar.getTimeInMillis() + millis); } return normalizer.apply(calendar.getTime()); }
@Override public Temporal addTo(Temporal temporal) { Jdk8Methods.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && chronology.equals(temporalChrono) == false) { throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId()); } if (years != 0) { temporal = temporal.plus(years, YEARS); } if (months != 0) { temporal = temporal.plus(months, MONTHS); } if (days != 0) { temporal = temporal.plus(days, DAYS); } return temporal; }
@Override public Temporal subtractFrom(Temporal temporal) { Jdk8Methods.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && chronology.equals(temporalChrono) == false) { throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId()); } if (years != 0) { temporal = temporal.minus(years, YEARS); } if (months != 0) { temporal = temporal.minus(months, MONTHS); } if (days != 0) { temporal = temporal.minus(days, DAYS); } return temporal; }