/** * Gets the local time part of this date-time. * <p> * This returns a local time with the same hour, minute, second and * nanosecond as this date-time. * * @return the time part of this date-time, not null */ public LocalTime toLocalTime() { return toLocalDateTime().toLocalTime(); }
/** * Converts this date-time to an {@code Instant}. * <p> * This combines this local date-time and the specified offset to form * an {@code Instant}. * * @param offset the offset to use for the conversion, not null * @return an {@code Instant} representing the same instant, not null */ public Instant toInstant(ZoneOffset offset) { return Instant.ofEpochSecond(toEpochSecond(offset), toLocalTime().getNano()); }
/** * A hash code for this date-time. * * @return a suitable hash code */ @Override public int hashCode() { return toLocalDate().hashCode() ^ toLocalTime().hashCode(); }
/** * Outputs this date-time as a {@code String}. * <p> * The output will include the full local date-time and the chronology ID. * * @return a string representation of this date-time, not null */ @Override public String toString() { return toLocalDate().toString() + 'T' + toLocalTime().toString(); }
/** * Checks if this date-time is equal to the specified date-time ignoring the chronology. * <p> * This method differs from the comparison in {@link #compareTo} in that it * only compares the underlying date and time and not the chronology. * This allows date-times in different calendar systems to be compared based * on the time-line position. * * @param other the other date-time to compare to, not null * @return true if the underlying date-time is equal to the specified date-time on the timeline */ public boolean isEqual(ChronoLocalDateTime<?> other) { // Do the time check first, it is cheaper than computing EPOCH day. return this.toLocalTime().toNanoOfDay() == other.toLocalTime().toNanoOfDay() && this.toLocalDate().toEpochDay() == other.toLocalDate().toEpochDay(); }
/** * Checks if this date-time is after the specified date-time ignoring the chronology. * <p> * This method differs from the comparison in {@link #compareTo} in that it * only compares the underlying date-time and not the chronology. * This allows dates in different calendar systems to be compared based * on the time-line position. * * @param other the other date-time to compare to, not null * @return true if this is after the specified date-time */ public boolean isAfter(ChronoLocalDateTime<?> other) { long thisEpDay = this.toLocalDate().toEpochDay(); long otherEpDay = other.toLocalDate().toEpochDay(); return thisEpDay > otherEpDay || (thisEpDay == otherEpDay && this.toLocalTime().toNanoOfDay() > other.toLocalTime().toNanoOfDay()); }
/** * Checks if this date-time is before the specified date-time ignoring the chronology. * <p> * This method differs from the comparison in {@link #compareTo} in that it * only compares the underlying date-time and not the chronology. * This allows dates in different calendar systems to be compared based * on the time-line position. * * @param other the other date-time to compare to, not null * @return true if this is before the specified date-time */ public boolean isBefore(ChronoLocalDateTime<?> other) { long thisEpDay = this.toLocalDate().toEpochDay(); long otherEpDay = other.toLocalDate().toEpochDay(); return thisEpDay < otherEpDay || (thisEpDay == otherEpDay && this.toLocalTime().toNanoOfDay() < other.toLocalTime().toNanoOfDay()); }
@Override public int compare(ChronoLocalDateTime<?> datetime1, ChronoLocalDateTime<?> datetime2) { int cmp = Jdk8Methods.compareLongs(datetime1.toLocalDate().toEpochDay(), datetime2.toLocalDate().toEpochDay()); if (cmp == 0) { cmp = Jdk8Methods.compareLongs(datetime1.toLocalTime().toNanoOfDay(), datetime2.toLocalTime().toNanoOfDay()); } return cmp; } };
@Override public Temporal adjustInto(Temporal temporal) { return temporal .with(EPOCH_DAY, toLocalDate().toEpochDay()) .with(NANO_OF_DAY, toLocalTime().toNanoOfDay()); }
int cmp = toLocalDate().compareTo(other.toLocalDate()); if (cmp == 0) { cmp = toLocalTime().compareTo(other.toLocalTime()); if (cmp == 0) { cmp = getChronology().compareTo(other.getChronology());
/** * Converts this date-time to the number of seconds from the epoch * of 1970-01-01T00:00:00Z. * <p> * This combines this local date-time and the specified offset to calculate the * epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z. * Instants on the time-line after the epoch are positive, earlier are negative. * * @param offset the offset to use for the conversion, not null * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z */ public long toEpochSecond(ZoneOffset offset) { Jdk8Methods.requireNonNull(offset, "offset"); long epochDay = toLocalDate().toEpochDay(); long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); secs -= offset.getTotalSeconds(); return secs; }
ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime<?>) resolvedObject; resolveMakeChanges(targetField, cldt.toLocalDate()); resolveMakeChanges(targetField, cldt.toLocalTime()); changes++; continue outer; // have to restart to avoid concurrent modification
@SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { if (query == TemporalQueries.chronology()) { return (R) getChronology(); } else if (query == TemporalQueries.precision()) { return (R) NANOS; } else if (query == TemporalQueries.localDate()) { return (R) LocalDate.ofEpochDay(toLocalDate().toEpochDay()); } else if (query == TemporalQueries.localTime()) { return (R) toLocalTime(); } else if (query == TemporalQueries.zone() || query == TemporalQueries.zoneId() || query == TemporalQueries.offset()) { return null; } return super.query(query); }
@Override public long until(Temporal endExclusive, TemporalUnit unit) { @SuppressWarnings("unchecked") ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) toLocalDate().getChronology().localDateTime(endExclusive); if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; if (f.isTimeBased()) { long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY); switch (f) { case NANOS: amount = Jdk8Methods.safeMultiply(amount, NANOS_PER_DAY); break; case MICROS: amount = Jdk8Methods.safeMultiply(amount, MICROS_PER_DAY); break; case MILLIS: amount = Jdk8Methods.safeMultiply(amount, MILLIS_PER_DAY); break; case SECONDS: amount = Jdk8Methods.safeMultiply(amount, SECONDS_PER_DAY); break; case MINUTES: amount = Jdk8Methods.safeMultiply(amount, MINUTES_PER_DAY); break; case HOURS: amount = Jdk8Methods.safeMultiply(amount, HOURS_PER_DAY); break; case HALF_DAYS: amount = Jdk8Methods.safeMultiply(amount, 2); break; } return Jdk8Methods.safeAdd(amount, time.until(end.toLocalTime(), unit)); } ChronoLocalDate endDate = end.toLocalDate(); if (end.toLocalTime().isBefore(time)) { endDate = endDate.minus(1, ChronoUnit.DAYS); } return date.until(endDate, unit); } return unit.between(this, end); }