@Override @SuppressWarnings("unchecked") public T next() { final T current = next; next = (T) next.plus(by, byUnit); num++; return current; } };
@Override @SuppressWarnings( "unchecked" ) public final V with( TemporalField field, long newValue ) { return replacement( (T) temporal().with( field, newValue ) ); }
/** * A utility method that adjusts <a href="https://dev.mysql.com/doc/refman/5.7/en/two-digit-years.html">ambiguous</a> 2-digit * year values of DATETIME, DATE, and TIMESTAMP types using these MySQL-specific rules: * <ul> * <li>Year values in the range 00-69 are converted to 2000-2069.</li> * <li>Year values in the range 70-99 are converted to 1970-1999.</li> * </ul> * * @param temporal the temporal instance to adjust; may not be null * @return the possibly adjusted temporal instance; never null */ protected static Temporal adjustTemporal(Temporal temporal) { if (temporal.isSupported(ChronoField.YEAR)) { int year = temporal.get(ChronoField.YEAR); if (0 <= year && year <= 69) { temporal = temporal.plus(2000, ChronoUnit.YEARS); } else if (70 <= year && year <= 99) { temporal = temporal.plus(1900, ChronoUnit.YEARS); } } return temporal; }
@SuppressWarnings( "unchecked" ) @Override public <R extends Temporal> R adjustInto( R temporal, long newValue ) { int newVal = range.checkValidIntValue( newValue, this ); int oldYear = temporal.get( ChronoField.YEAR ); return (R) temporal.with( ChronoField.YEAR, (oldYear / years) * years + newVal ) .with( TemporalAdjusters.firstDayOfYear() ); }
long nanos; boolean negate = false; if ( from.isSupported( OFFSET_SECONDS ) && !to.isSupported( OFFSET_SECONDS ) ) int fromNanos = from.isSupported( NANO_OF_SECOND ) ? from.get( NANO_OF_SECOND ) : 0; int toNanos = to.isSupported( NANO_OF_SECOND ) ? to.get( NANO_OF_SECOND ) : 0; nanos = toNanos - fromNanos; && from.isSupported( SECOND_OF_MINUTE ) && to.isSupported( SECOND_OF_MINUTE ) && from.get( SECOND_OF_MINUTE ) != to.get( SECOND_OF_MINUTE );
@Override public final int get( TemporalField field ) { int accessor; try { accessor = temporal().get( field ); } catch ( UnsupportedTemporalTypeException e ) { throw new UnsupportedTemporalUnitException( e.getMessage(), e ); } return accessor; }
@Nullable Temporal isOk(Temporal t) { if (checker.matches(t)) { return null; } Temporal out = t.plus(1, type.getBaseUnit()); // Fall-through switch case. for example if type is year all cases below must also be handled. switch (type) { case YEAR: out = out.with(ChronoField.MONTH_OF_YEAR, 1); case MONTH_OF_YEAR: out = out.with(ChronoField.DAY_OF_MONTH, 1); case DAY_OF_WEEK: case DAY_OF_MONTH: out = out.with(ChronoField.HOUR_OF_DAY, 0); case HOUR_OF_DAY: out = out.with(ChronoField.MINUTE_OF_HOUR, 0); case MINUTE_OF_HOUR: out = out.with(ChronoField.SECOND_OF_MINUTE, 0); case SECOND_OF_MINUTE: return out; default: throw new IllegalArgumentException("Invalid field type " + type); } }
public static TemporalAdjuster startOfNextOddHour() { return temporal -> { int currentHour = temporal.get(ChronoField.HOUR_OF_DAY); return temporal.plus((currentHour % 2 == 0) ? 1 : 2, ChronoUnit.HOURS) .with(ChronoField.MINUTE_OF_HOUR, 0) .with(ChronoField.SECOND_OF_MINUTE, 0) .with(ChronoField.NANO_OF_SECOND, 0); }; }
private Temporal computeDateForMonthlyFrequencyFrom(final Temporal source, DayOfWeekOccurrence dayOfWeek) { Temporal current = source; if (dayOfWeek.nth() > 1) { current = current.with(ChronoField.ALIGNED_WEEK_OF_MONTH, dayOfWeek.nth()); } else if (dayOfWeek.nth() < 0) { current = current.with(ChronoField.DAY_OF_MONTH, 1) .plus(1, ChronoUnit.MONTHS) .minus(1, ChronoUnit.DAYS) .plus(dayOfWeek.nth(), ChronoUnit.WEEKS) .with(dayOfWeek.dayOfWeek()); } return current; }
@Override public boolean contains(Temporal_ value) { if (value == null || !value.isSupported(incrementUnitType)) { return false; } // We cannot use Temporal.until() to check bounds due to rounding errors if (value.compareTo(from) < 0 || value.compareTo(to) >= 0) { return false; } long fromSpace = from.until(value, incrementUnitType); if (value.equals(from.plus(fromSpace + 1, incrementUnitType))) { // Temporal.until() rounds down, but it needs to round up, to be consistent with Temporal.plus() fromSpace++; } // Only checking the modulus is not enough: 1-MAR + 1 month doesn't include 7-MAR but the modulus is 0 anyway return fromSpace % incrementUnitAmount == 0 && value.equals(from.plus(fromSpace, incrementUnitType)); }
@Override public Temporal adjustInto(Temporal temporal) { int calDow = temporal.get(DAY_OF_WEEK); if (relative < 2 && calDow == dowValue) { return temporal; } if ((relative & 1) == 0) { int daysDiff = calDow - dowValue; return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS); } else { int daysDiff = dowValue - calDow; return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS); } } }
private static Temporal assertValidMinus( Temporal temporal, long amountToAdd, TemporalUnit unit ) { try { return temporal.minus(amountToAdd, unit); } catch ( DateTimeException | ArithmeticException e ) { throw new TemporalArithmeticException( e.getMessage(), e ); } }
public static TemporalAdjuster startOfPreviousEvenHour() { return temporal -> { int currentHour = temporal.get(ChronoField.HOUR_OF_DAY); return temporal.minus((currentHour % 2 == 0) ? 0 : 1, ChronoUnit.HOURS) .with(ChronoField.MINUTE_OF_HOUR, 0) .with(ChronoField.SECOND_OF_MINUTE, 0) .with(ChronoField.NANO_OF_SECOND, 0); }; }
static DurationValue durationBetween( Temporal from, Temporal to ) { long months = 0; long days = 0; if ( from.isSupported( EPOCH_DAY ) && to.isSupported( EPOCH_DAY ) ) { months = assertValidUntil( from, to, ChronoUnit.MONTHS ); try { from = from.plus( months, ChronoUnit.MONTHS ); } catch ( DateTimeException | ArithmeticException e ) { throw new TemporalArithmeticException( e.getMessage(), e ); } days = assertValidUntil( from, to, ChronoUnit.DAYS ); try { from = from.plus( days, ChronoUnit.DAYS ); } catch ( DateTimeException | ArithmeticException e ) { throw new TemporalArithmeticException( e.getMessage(), e ); } } long nanos = assertValidUntil( from, to, NANOS ); return newDuration( months, days, nanos / NANOS_PER_SECOND, nanos % NANOS_PER_SECOND ); }
@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()); }
public static TemporalAdjuster nextOrSameDayOfMonth(int dayOfMonth) { validateDayOfMonth(dayOfMonth); TemporalAdjuster nextDayOfMonth = nextDayOfMonth(dayOfMonth); return temporal -> { int currentDayOfMonth = temporal.get(DAY_OF_MONTH); if (currentDayOfMonth == dayOfMonth || (currentDayOfMonth < dayOfMonth && currentDayOfMonth == temporal.range(DAY_OF_MONTH).getMaximum())) { return temporal; } return temporal.with(nextDayOfMonth); }; }
@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 final boolean isSupported( TemporalField field ) { return temporal().isSupported( field ); }