@Override public final boolean isSupported( TemporalField field ) { return temporal().isSupported( field ); }
@Override public final boolean isSupported( TemporalUnit unit ) { return temporal().isSupported( unit ); }
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 Temporal subtractFrom( Temporal temporal ) if ( months != 0 && temporal.isSupported( MONTHS ) ) if ( days != 0 && temporal.isSupported( DAYS ) ) if ( temporal.isSupported( SECONDS ) ) else if ( temporal.isSupported( DAYS ) ) if ( nanos != 0 && temporal.isSupported( NANOS ) )
@Override public Temporal addTo( Temporal temporal ) if ( months != 0 && temporal.isSupported( MONTHS ) ) if ( days != 0 && temporal.isSupported( DAYS ) ) if ( temporal.isSupported( SECONDS ) ) if ( nanos != 0 && temporal.isSupported( NANOS ) )
@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)); }
+ " must have strictly positive incrementUnitAmount (" + incrementUnitAmount + ")."); if (!from.isSupported(incrementUnitType) || !to.isSupported(incrementUnitType)) { throw new IllegalArgumentException("The " + getClass().getSimpleName() + " must have an incrementUnitType (" + incrementUnitType
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 ); }
/** * 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; }
@Override public boolean isSupportedBy(Temporal temporal) { return temporal.isSupported(this); }
@Override public boolean isSupportedBy(Temporal temporal) { return temporal.isSupported(EPOCH_DAY); }
/** * Get the temporal macro-type of given <code>temporal</code> instance, if available. * @param temporal Temporal object * @return {@link TemporalType} of given {@link Temporal}, empty if given temporal is <code>null</code> */ public static Optional<TemporalType> getTemporalType(Temporal temporal) { if (temporal != null) { if (temporal.isSupported(ChronoUnit.HOURS)) { return Optional.of((temporal.isSupported(ChronoUnit.DAYS)) ? DATE_TIME : TIME); } return Optional.of(DATE); } return Optional.empty(); }
@Override public final boolean isSupported( TemporalUnit unit ) { return temporal().isSupported( unit ); }
@Override public final boolean isSupported( TemporalField field ) { return temporal().isSupported( field ); }
/** * @see #toLocalized(Temporal, String) * @return only time part if time exists, empty otherwise. */ public static String toLocalizedTime(final Temporal temporal, final String language) { Objects.requireNonNull(temporal); Objects.requireNonNull(language); final String result; if (temporal.isSupported(ChronoUnit.HOURS)) { result = DateTimeFormatter.ofPattern(getTimePattern(language)).format(temporal); } else { result = ""; } return result; }
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 );
private static final long millis(Temporal temporal) { // java.sql.* temporal types: if (temporal instanceof LocalDate) { return Date.valueOf((LocalDate) temporal).getTime(); } else if (temporal instanceof LocalTime) { return Time.valueOf((LocalTime) temporal).getTime(); } else if (temporal instanceof LocalDateTime) { return Timestamp.valueOf((LocalDateTime) temporal).getTime(); } // OffsetDateTime else if (temporal.isSupported(INSTANT_SECONDS)) { return 1000 * temporal.getLong(INSTANT_SECONDS) + temporal.getLong(MILLI_OF_SECOND); } // OffsetTime else if (temporal.isSupported(MILLI_OF_DAY)) { return temporal.getLong(MILLI_OF_DAY); } throw fail(temporal, Long.class); }
@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()); }
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 ); }
/** * @see #toLocalized(Temporal, ZoneId, String) * @return only time part if time exists, empty otherwise. */ public static String toLocalizedTime(final Temporal temporal, final ZoneId zoneId, final String language) { if (temporal.isSupported(ChronoUnit.HOURS) && temporal.get(ChronoField.OFFSET_SECONDS) != ZonedDateTime.now(zoneId).get(ChronoField.OFFSET_SECONDS)) { final ZoneId actualZoneId = asZonedDateTime(temporal).getZone(); final String pattern = getTimePattern(language); return toZonedFormat(pattern, temporal, actualZoneId); } else { return toLocalizedTime(temporal, language); } }