@Override @SuppressWarnings("unchecked") public T next() { final T current = next; next = (T) next.plus(by, byUnit); num++; return current; } };
@Override public Temporal_ get(long index) { if (index >= size || index < 0) { throw new IndexOutOfBoundsException(); } return (Temporal_) from.plus(index * incrementUnitAmount, incrementUnitType); }
private static Temporal assertValidPlus( Temporal temporal, long amountToAdd, TemporalUnit unit ) { try { return temporal.plus(amountToAdd, unit); } catch ( DateTimeException | ArithmeticException e ) { throw new TemporalArithmeticException( e.getMessage(), e ); } }
@Override @SuppressWarnings( "unchecked" ) public final V plus( long amountToAdd, TemporalUnit unit ) { return replacement( (T) temporal().plus( amountToAdd, unit ) ); }
@Override @SuppressWarnings( "unchecked" ) public final V plus( TemporalAmount amount ) { return replacement( (T) temporal().plus( amount ) ); }
@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)); }
/** * 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; }
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 public Temporal adjustInto(@Nullable Temporal temporal) { if (timeDone == null) { timeDone = temporal; } if (iterator.hasNext()) { current = iterator.next(); } Temporal nextTime = timeDone.plus(current); timeDone = nextTime; return nextTime; }
@Override public Temporal adjustInto(@Nullable Temporal temporal) { // Never match the actual time, so since our basic // unit is seconds, we add one second. Temporal ret = temporal.plus(1, ChronoUnit.SECONDS); // We loop through the fields until they all match. If // one of them does not match, its type is incremented // and all lower fields are reset to their minimum. And // we start over with this new time. int index = 0; int length = fields.size(); while (index < length) { final Field field = fields.get(index); final Temporal out = field.isOk(ret); if (out == null) { index++; } else { ret = out; index = 0; } } // All fields match! return ret; }
@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); } }
@Override @SuppressWarnings( "unchecked" ) public final V plus( long amountToAdd, TemporalUnit unit ) { return replacement( (T) temporal().plus( amountToAdd, unit ) ); }
/** * Returns the "plus working days" adjuster, which returns a new date set for the specified number * of working days (defined as all days that are not Saturdays, Sundays, or BYU University * Holidays) in the future. * * @param workingDays the number of working days to be added to a Temporal object * @return the temporal adjuster as described */ public static TemporalAdjuster plusWorkingDays(int workingDays) { return temporal -> temporal.plus(toCalendarDays(toLocalDate(temporal), workingDays), ChronoUnit.DAYS); }
@Override @SuppressWarnings( "unchecked" ) public final V plus( TemporalAmount amount ) { return replacement( (T) temporal().plus( amount ) ); }
@Override public Temporal adjustInto(Temporal temporal) { Temporal adjusted = temporal.with(adjuster); return (ChronoUnit.NANOS.between(temporal, adjusted) >= 0) ? adjusted : temporal.plus(amount).with(adjuster); } };
@SuppressWarnings("unchecked") @Override public <R extends Temporal> R adjustInto(R temporal, long newValue) { range().checkValidValue(newValue, this); return (R) temporal.plus(Jdk8Methods.safeSubtract(newValue, getFrom(temporal)), WEEKS); } @Override
@Override public Temporal adjustInto(Temporal temporal) { switch (ordinal) { case 0: return temporal.with(DAY_OF_MONTH, 1); case 1: return temporal.with(DAY_OF_MONTH, temporal.range(DAY_OF_MONTH).getMaximum()); case 2: return temporal.with(DAY_OF_MONTH, 1).plus(1, MONTHS); case 3: return temporal.with(DAY_OF_YEAR, 1); case 4: return temporal.with(DAY_OF_YEAR, temporal.range(DAY_OF_YEAR).getMaximum()); case 5: return temporal.with(DAY_OF_YEAR, 1).plus(1, YEARS); } throw new IllegalStateException("Unreachable"); } }