/** * Converts this date to the Epoch Day. * <p> * The {@link ChronoField#EPOCH_DAY Epoch Day count} is a simple * incrementing count of days where day 0 is 1970-01-01 (ISO). * This definition is the same for all chronologies, enabling conversion. * * @return the Epoch Day equivalent to this date */ public long toEpochDay() { return getLong(EPOCH_DAY); }
/** * Outputs this date as a {@code String}. * <p> * The output will include the full local date and the chronology ID. * * @return the formatted date, not null */ @Override public String toString() { // getLong() reduces chances of exceptions in toString() long yoe = getLong(YEAR_OF_ERA); long moy = getLong(MONTH_OF_YEAR); long dom = getLong(DAY_OF_MONTH); StringBuilder buf = new StringBuilder(30); buf.append(getChronology().toString()) .append(" ") .append(getEra()) .append(" ") .append(yoe) .append(moy < 10 ? "-0" : "-").append(moy) .append(dom < 10 ? "-0" : "-").append(dom); return buf.toString(); }
@Override public long getLong(TemporalField field) { if (effectiveDate != null && field.isDateBased()) { return effectiveDate.getLong(field); } return temporal.getLong(field); } @SuppressWarnings("unchecked")
/** * Checks if the year is a leap year, as defined by the calendar system. * <p> * A leap-year is a year of a longer length than normal. * The exact meaning is determined by the chronology with the constraint that * a leap-year must imply a year-length longer than a non leap-year. * <p> * The default implementation uses {@link Chronology#isLeapYear(long)}. * * @return true if this date is in a leap year, false otherwise */ public boolean isLeapYear() { return getChronology().isLeapYear(getLong(YEAR)); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { return (field.isTimeBased() ? time.getLong(field) : date.getLong(field)); } return field.getFrom(this); }
@Override public long getLong(TemporalField field) { Jdk8Methods.requireNonNull(field, "field"); Long value = getFieldValue0(field); if (value == null) { if (date != null && date.isSupported(field)) { return date.getLong(field); } if (time != null && time.isSupported(field)) { return time.getLong(field); } throw new DateTimeException("Field not found: " + field); } return value; }
if (date.getLong(this) != fieldValues.get(this)) { throw new DateTimeException("Strict mode rejected date parsed to a different year"); if (date.getLong(MONTH_OF_YEAR) != fieldValues.get(MONTH_OF_YEAR)) { throw new DateTimeException("Strict mode rejected date parsed to a different month"); if (date.getLong(YEAR) != fieldValues.get(YEAR)) { throw new DateTimeException("Strict mode rejected date parsed to a different year");
@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); }