/** * Outputs this date-time as a {@code String}. * <p> * The output will include the full zoned date-time and the chronology ID. * * @return a string representation of this date-time, not null */ @Override public String toString() { String str = toLocalDateTime().toString() + getOffset().toString(); if (getOffset() != getZone()) { str += '[' + getZone().toString() + ']'; } return str; }
/** * A hash code for this date-time. * * @return a suitable hash code */ @Override public int hashCode() { return toLocalDateTime().hashCode() ^ getOffset().hashCode() ^ Integer.rotateLeft(getZone().hashCode(), 3); }
final long ms = ((ChronoZonedDateTime<?>) value).toInstant().toEpochMilli(); final long offsetInMilliseconds = ((ChronoZonedDateTime<?>) value).getOffset().getTotalSeconds() * 1000L; value = ms + offsetInMilliseconds;
@Override public int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new UnsupportedTemporalTypeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().get(field); } return super.get(field); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().getLong(field); } return field.getFrom(this); }
/** * Converts this date-time to the number of seconds from the epoch * of 1970-01-01T00:00:00Z. * <p> * This uses the {@linkplain #toLocalDateTime() local date-time} and * {@linkplain #getOffset() 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. * * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z */ public long toEpochSecond() { long epochDay = toLocalDate().toEpochDay(); long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); secs -= getOffset().getTotalSeconds(); return secs; }
@SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { if (query == TemporalQueries.zoneId() || query == TemporalQueries.zone()) { return (R) getZone(); } else if (query == TemporalQueries.chronology()) { return (R) toLocalDate().getChronology(); } else if (query == TemporalQueries.precision()) { return (R) NANOS; } else if (query == TemporalQueries.offset()) { return (R) getOffset(); } else if (query == TemporalQueries.localDate()) { return (R) LocalDate.ofEpochDay(toLocalDate().toEpochDay()); } else if (query == TemporalQueries.localTime()) { return (R) toLocalTime(); } return super.query(query); }