src/share/classes/java/time/temporal/JulianFields.java

Print this page

        

*** 63,84 **** import static java.time.temporal.ChronoField.EPOCH_DAY; import static java.time.temporal.ChronoUnit.DAYS; import static java.time.temporal.ChronoUnit.FOREVER; - import java.io.InvalidObjectException; - import java.io.Serializable; import java.time.DateTimeException; ! import java.time.LocalDate; ! import java.time.format.DateTimeBuilder; /** * A set of date fields that provide access to Julian Days. * <p> * The Julian Day is a standard way of expressing date and time commonly used in the scientific community. * It is expressed as a decimal number of whole days where days start at midday. * This class represents variations on Julian Days that count whole days from midnight. * * <h3>Specification for implementors</h3> * This is an immutable and thread-safe class. * * @since 1.8 --- 63,86 ---- import static java.time.temporal.ChronoField.EPOCH_DAY; import static java.time.temporal.ChronoUnit.DAYS; import static java.time.temporal.ChronoUnit.FOREVER; import java.time.DateTimeException; ! import java.util.Collections; ! import java.util.Map; /** * A set of date fields that provide access to Julian Days. * <p> * The Julian Day is a standard way of expressing date and time commonly used in the scientific community. * It is expressed as a decimal number of whole days where days start at midday. * This class represents variations on Julian Days that count whole days from midnight. + * <p> + * The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}. + * The fields are supported, and can be queried and set if {@code EPOCH_DAY} is available. + * The fields work with all chronologies. * * <h3>Specification for implementors</h3> * This is an immutable and thread-safe class. * * @since 1.8
*** 97,110 **** * Julian Day is a well-known system that represents the count of whole days since day 0, * which is defined to be January 1, 4713 BCE in the Julian calendar, and -4713-11-24 Gregorian. * The field has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'. * The field always refers to the local date-time, ignoring the offset or zone. * <p> ! * For date-times, 'JULIAN_DAY.doGet()' assumes the same value from * midnight until just before the next midnight. ! * When 'JULIAN_DAY.doWith()' is applied to a date-time, the time of day portion remains unaltered. ! * 'JULIAN_DAY.doWith()' and 'JULIAN_DAY.doGet()' only apply to {@code Temporal} objects that * can be converted into {@link ChronoField#EPOCH_DAY}. * A {@link DateTimeException} is thrown for any other type of object. * <p> * <h3>Astronomical and Scientific Notes</h3> * The standard astronomical definition uses a fraction to indicate the time-of-day, --- 99,112 ---- * Julian Day is a well-known system that represents the count of whole days since day 0, * which is defined to be January 1, 4713 BCE in the Julian calendar, and -4713-11-24 Gregorian. * The field has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'. * The field always refers to the local date-time, ignoring the offset or zone. * <p> ! * For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from * midnight until just before the next midnight. ! * When 'JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. ! * 'JULIAN_DAY.adjustInto()' and 'JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects that * can be converted into {@link ChronoField#EPOCH_DAY}. * A {@link DateTimeException} is thrown for any other type of object. * <p> * <h3>Astronomical and Scientific Notes</h3> * The standard astronomical definition uses a fraction to indicate the time-of-day,
*** 127,151 **** * <p> * Julian Days are sometimes taken to imply Universal Time or UTC, but this * implementation always uses the Julian Day number for the local date, * regardless of the offset or time-zone. */ ! public static final TemporalField JULIAN_DAY = new Field("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET); /** * Modified Julian Day field. * <p> * This is an integer-based version of the Modified Julian Day Number. * Modified Julian Day (MJD) is a well-known system that counts days continuously. * It is defined relative to astronomical Julian Day as {@code MJD = JD - 2400000.5}. * Each Modified Julian Day runs from midnight to midnight. * The field always refers to the local date-time, ignoring the offset or zone. * <p> ! * For date-times, 'MODIFIED_JULIAN_DAY.doGet()' assumes the same value from * midnight until just before the next midnight. ! * When 'MODIFIED_JULIAN_DAY.doWith()' is applied to a date-time, the time of day portion remains unaltered. ! * 'MODIFIED_JULIAN_DAY.doWith()' and 'MODIFIED_JULIAN_DAY.doGet()' only apply to {@code Temporal} objects * that can be converted into {@link ChronoField#EPOCH_DAY}. * A {@link DateTimeException} is thrown for any other type of object. * <p> * This implementation is an integer version of MJD with the decimal part rounded to floor. * <p> --- 129,153 ---- * <p> * Julian Days are sometimes taken to imply Universal Time or UTC, but this * implementation always uses the Julian Day number for the local date, * regardless of the offset or time-zone. */ ! public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY; /** * Modified Julian Day field. * <p> * This is an integer-based version of the Modified Julian Day Number. * Modified Julian Day (MJD) is a well-known system that counts days continuously. * It is defined relative to astronomical Julian Day as {@code MJD = JD - 2400000.5}. * Each Modified Julian Day runs from midnight to midnight. * The field always refers to the local date-time, ignoring the offset or zone. * <p> ! * For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from * midnight until just before the next midnight. ! * When 'MODIFIED_JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. ! * 'MODIFIED_JULIAN_DAY.adjustInto()' and 'MODIFIED_JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects * that can be converted into {@link ChronoField#EPOCH_DAY}. * A {@link DateTimeException} is thrown for any other type of object. * <p> * This implementation is an integer version of MJD with the decimal part rounded to floor. * <p>
*** 163,204 **** * <p> * Modified Julian Days are sometimes taken to imply Universal Time or UTC, but this * implementation always uses the Modified Julian Day for the local date, * regardless of the offset or time-zone. */ ! public static final TemporalField MODIFIED_JULIAN_DAY = new Field("ModifiedJulianDay", DAYS, FOREVER, 40587L); /** * Rata Die field. * <p> * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO). * The field always refers to the local date-time, ignoring the offset or zone. * <p> ! * For date-times, 'RATA_DIE.doGet()' assumes the same value from * midnight until just before the next midnight. ! * When 'RATA_DIE.doWith()' is applied to a date-time, the time of day portion remains unaltered. ! * 'MODIFIED_JULIAN_DAY.doWith()' and 'RATA_DIE.doGet()' only apply to {@code Temporal} objects * that can be converted into {@link ChronoField#EPOCH_DAY}. * A {@link DateTimeException} is thrown for any other type of object. */ ! public static final TemporalField RATA_DIE = new Field("RataDie", DAYS, FOREVER, 719163L); /** * Restricted constructor. */ private JulianFields() { throw new AssertionError("Not instantiable"); } /** ! * implementation of JulianFields. Each instance is a singleton. */ ! private static class Field implements TemporalField, Serializable { private static final long serialVersionUID = -7501623920830201812L; ! private final String name; private final transient TemporalUnit baseUnit; private final transient TemporalUnit rangeUnit; private final transient ValueRange range; private final transient long offset; --- 165,209 ---- * <p> * Modified Julian Days are sometimes taken to imply Universal Time or UTC, but this * implementation always uses the Modified Julian Day for the local date, * regardless of the offset or time-zone. */ ! public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY; /** * Rata Die field. * <p> * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO). * The field always refers to the local date-time, ignoring the offset or zone. * <p> ! * For date-times, 'RATA_DIE.getFrom()' assumes the same value from * midnight until just before the next midnight. ! * When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. ! * 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects * that can be converted into {@link ChronoField#EPOCH_DAY}. * A {@link DateTimeException} is thrown for any other type of object. */ ! public static final TemporalField RATA_DIE = Field.RATA_DIE; /** * Restricted constructor. */ private JulianFields() { throw new AssertionError("Not instantiable"); } /** ! * Implementation of JulianFields. Each instance is a singleton. */ ! private static enum Field implements TemporalField { ! JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET), ! MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L), ! RATA_DIE("RataDie", DAYS, FOREVER, 719163L); private static final long serialVersionUID = -7501623920830201812L; ! private final transient String name; private final transient TemporalUnit baseUnit; private final transient TemporalUnit rangeUnit; private final transient ValueRange range; private final transient long offset;
*** 208,236 **** this.rangeUnit = rangeUnit; this.range = ValueRange.of(-365243219162L + offset, 365241780471L + offset); this.offset = offset; } - - /** - * Resolve the object from the stream to the appropriate singleton. - * @return one of the singleton objects {@link #JULIAN_DAY}, - * {@link #MODIFIED_JULIAN_DAY}, or {@link #RATA_DIE}. - * @throws InvalidObjectException if the object in the stream is not one of the singletons. - */ - private Object readResolve() throws InvalidObjectException { - if (JULIAN_DAY.getName().equals(name)) { - return JULIAN_DAY; - } else if (MODIFIED_JULIAN_DAY.getName().equals(name)) { - return MODIFIED_JULIAN_DAY; - } else if (RATA_DIE.getName().equals(name)) { - return RATA_DIE; - } else { - throw new InvalidObjectException("Not one of the singletons"); - } - } - //----------------------------------------------------------------------- @Override public String getName() { return name; } --- 213,222 ----
*** 250,305 **** return range; } //----------------------------------------------------------------------- @Override ! public boolean doIsSupported(TemporalAccessor temporal) { return temporal.isSupported(EPOCH_DAY); } @Override ! public ValueRange doRange(TemporalAccessor temporal) { ! if (doIsSupported(temporal) == false) { throw new DateTimeException("Unsupported field: " + this); } return range(); } @Override ! public long doGet(TemporalAccessor temporal) { return temporal.getLong(EPOCH_DAY) + offset; } @Override ! public <R extends Temporal> R doWith(R temporal, long newValue) { if (range().isValidValue(newValue) == false) { throw new DateTimeException("Invalid value: " + name + " " + newValue); } return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset)); } //----------------------------------------------------------------------- @Override ! public boolean resolve(DateTimeBuilder builder, long value) { ! boolean changed = false; ! changed = resolve0(JULIAN_DAY, builder, changed); ! changed = resolve0(MODIFIED_JULIAN_DAY, builder, changed); ! changed = resolve0(RATA_DIE, builder, changed); ! return changed; ! } ! ! private boolean resolve0(TemporalField field, DateTimeBuilder builder, boolean changed) { ! if (builder.containsFieldValue(field)) { ! builder.addCalendrical(LocalDate.ofEpochDay(Math.subtractExact(builder.getFieldValue(JULIAN_DAY), JULIAN_DAY_OFFSET))); ! builder.removeFieldValue(JULIAN_DAY); ! changed = true; ! } ! return changed; } //----------------------------------------------------------------------- @Override public String toString() { ! return getName(); } } } --- 236,279 ---- return range; } //----------------------------------------------------------------------- @Override ! public boolean isSupportedBy(TemporalAccessor temporal) { return temporal.isSupported(EPOCH_DAY); } @Override ! public ValueRange rangeRefinedBy(TemporalAccessor temporal) { ! if (isSupportedBy(temporal) == false) { throw new DateTimeException("Unsupported field: " + this); } return range(); } @Override ! public long getFrom(TemporalAccessor temporal) { return temporal.getLong(EPOCH_DAY) + offset; } + @SuppressWarnings("unchecked") @Override ! public <R extends Temporal> R adjustInto(R temporal, long newValue) { if (range().isValidValue(newValue) == false) { throw new DateTimeException("Invalid value: " + name + " " + newValue); } return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset)); } //----------------------------------------------------------------------- @Override ! public Map<TemporalField, Long> resolve(TemporalAccessor temporal, long value) { ! return Collections.<TemporalField, Long>singletonMap(EPOCH_DAY, Math.subtractExact(value, offset)); } //----------------------------------------------------------------------- @Override public String toString() { ! return name; } } }