49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time.temporal; 63 64 import static java.time.temporal.ChronoField.EPOCH_DAY; 65 import static java.time.temporal.ChronoUnit.DAYS; 66 import static java.time.temporal.ChronoUnit.FOREVER; 67 68 import java.time.DateTimeException; 69 import java.util.Collections; 70 import java.util.Map; 71 72 /** 73 * A set of date fields that provide access to Julian Days. 74 * <p> 75 * The Julian Day is a standard way of expressing date and time commonly used in the scientific community. 76 * It is expressed as a decimal number of whole days where days start at midday. 77 * This class represents variations on Julian Days that count whole days from midnight. 78 * <p> 79 * The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}. 80 * The fields are supported, and can be queried and set if {@code EPOCH_DAY} is available. 81 * The fields work with all chronologies. 82 * 83 * <h3>Specification for implementors</h3> 84 * This is an immutable and thread-safe class. 85 * 86 * @since 1.8 87 */ 88 public final class JulianFields { 89 90 /** 91 * The offset from Julian to EPOCH DAY. 92 */ 93 private static final long JULIAN_DAY_OFFSET = 2440588L; 94 95 /** 96 * Julian Day field. 97 * <p> 98 * This is an integer-based version of the Julian Day Number. 99 * Julian Day is a well-known system that represents the count of whole days since day 0, 100 * which is defined to be January 1, 4713 BCE in the Julian calendar, and -4713-11-24 Gregorian. 101 * The field has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'. 102 * The field always refers to the local date-time, ignoring the offset or zone. 103 * <p> 104 * For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from 105 * midnight until just before the next midnight. 106 * When 'JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. 107 * 'JULIAN_DAY.adjustInto()' and 'JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects that 108 * can be converted into {@link ChronoField#EPOCH_DAY}. 109 * A {@link DateTimeException} is thrown for any other type of object. 110 * <p> 111 * <h3>Astronomical and Scientific Notes</h3> 112 * The standard astronomical definition uses a fraction to indicate the time-of-day, 113 * thus 3.25 would represent the time 18:00, since days start at midday. 114 * This implementation uses an integer and days starting at midnight. 115 * The integer value for the Julian Day Number is the astronomical Julian Day value at midday 116 * of the date in question. 117 * This amounts to the astronomical Julian Day, rounded to an integer {@code JDN = floor(JD + 0.5)}. 118 * <p> 119 * <pre> 120 * | ISO date | Julian Day Number | Astronomical Julian Day | 121 * | 1970-01-01T00:00 | 2,440,588 | 2,440,587.5 | 122 * | 1970-01-01T06:00 | 2,440,588 | 2,440,587.75 | 123 * | 1970-01-01T12:00 | 2,440,588 | 2,440,588.0 | 124 * | 1970-01-01T18:00 | 2,440,588 | 2,440,588.25 | 125 * | 1970-01-02T00:00 | 2,440,589 | 2,440,588.5 | 126 * | 1970-01-02T06:00 | 2,440,589 | 2,440,588.75 | 127 * | 1970-01-02T12:00 | 2,440,589 | 2,440,589.0 | 128 * </pre> 129 * <p> 130 * Julian Days are sometimes taken to imply Universal Time or UTC, but this 131 * implementation always uses the Julian Day number for the local date, 132 * regardless of the offset or time-zone. 133 */ 134 public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY; 135 136 /** 137 * Modified Julian Day field. 138 * <p> 139 * This is an integer-based version of the Modified Julian Day Number. 140 * Modified Julian Day (MJD) is a well-known system that counts days continuously. 141 * It is defined relative to astronomical Julian Day as {@code MJD = JD - 2400000.5}. 142 * Each Modified Julian Day runs from midnight to midnight. 143 * The field always refers to the local date-time, ignoring the offset or zone. 144 * <p> 145 * For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from 146 * midnight until just before the next midnight. 147 * When 'MODIFIED_JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. 148 * 'MODIFIED_JULIAN_DAY.adjustInto()' and 'MODIFIED_JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects 149 * that can be converted into {@link ChronoField#EPOCH_DAY}. 150 * A {@link DateTimeException} is thrown for any other type of object. 151 * <p> 152 * This implementation is an integer version of MJD with the decimal part rounded to floor. 153 * <p> 154 * <h3>Astronomical and Scientific Notes</h3> 155 * <pre> 156 * | ISO date | Modified Julian Day | Decimal MJD | 157 * | 1970-01-01T00:00 | 40,587 | 40,587.0 | 158 * | 1970-01-01T06:00 | 40,587 | 40,587.25 | 159 * | 1970-01-01T12:00 | 40,587 | 40,587.5 | 160 * | 1970-01-01T18:00 | 40,587 | 40,587.75 | 161 * | 1970-01-02T00:00 | 40,588 | 40,588.0 | 162 * | 1970-01-02T06:00 | 40,588 | 40,588.25 | 163 * | 1970-01-02T12:00 | 40,588 | 40,588.5 | 164 * </pre> 165 * <p> 166 * Modified Julian Days are sometimes taken to imply Universal Time or UTC, but this 167 * implementation always uses the Modified Julian Day for the local date, 168 * regardless of the offset or time-zone. 169 */ 170 public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY; 171 172 /** 173 * Rata Die field. 174 * <p> 175 * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO). 176 * The field always refers to the local date-time, ignoring the offset or zone. 177 * <p> 178 * For date-times, 'RATA_DIE.getFrom()' assumes the same value from 179 * midnight until just before the next midnight. 180 * When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. 181 * 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects 182 * that can be converted into {@link ChronoField#EPOCH_DAY}. 183 * A {@link DateTimeException} is thrown for any other type of object. 184 */ 185 public static final TemporalField RATA_DIE = Field.RATA_DIE; 186 187 /** 188 * Restricted constructor. 189 */ 190 private JulianFields() { 191 throw new AssertionError("Not instantiable"); 192 } 193 194 /** 195 * Implementation of JulianFields. Each instance is a singleton. 196 */ 197 private static enum Field implements TemporalField { 198 JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET), 199 MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L), 200 RATA_DIE("RataDie", DAYS, FOREVER, 719163L); 201 202 private static final long serialVersionUID = -7501623920830201812L; 203 215 this.offset = offset; 216 } 217 218 //----------------------------------------------------------------------- 219 @Override 220 public String getName() { 221 return name; 222 } 223 224 @Override 225 public TemporalUnit getBaseUnit() { 226 return baseUnit; 227 } 228 229 @Override 230 public TemporalUnit getRangeUnit() { 231 return rangeUnit; 232 } 233 234 @Override 235 public ValueRange range() { 236 return range; 237 } 238 239 //----------------------------------------------------------------------- 240 @Override 241 public boolean isSupportedBy(TemporalAccessor temporal) { 242 return temporal.isSupported(EPOCH_DAY); 243 } 244 245 @Override 246 public ValueRange rangeRefinedBy(TemporalAccessor temporal) { 247 if (isSupportedBy(temporal) == false) { 248 throw new DateTimeException("Unsupported field: " + this); 249 } 250 return range(); 251 } 252 253 @Override 254 public long getFrom(TemporalAccessor temporal) { 255 return temporal.getLong(EPOCH_DAY) + offset; 256 } 257 258 @SuppressWarnings("unchecked") 259 @Override 260 public <R extends Temporal> R adjustInto(R temporal, long newValue) { 261 if (range().isValidValue(newValue) == false) { 262 throw new DateTimeException("Invalid value: " + name + " " + newValue); 263 } 264 return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset)); 265 } 266 267 //----------------------------------------------------------------------- 268 @Override 269 public Map<TemporalField, Long> resolve(TemporalAccessor temporal, long value) { 270 return Collections.<TemporalField, Long>singletonMap(EPOCH_DAY, Math.subtractExact(value, offset)); 271 } 272 273 //----------------------------------------------------------------------- 274 @Override 275 public String toString() { 276 return name; 277 } 278 } 279 } | 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time.temporal; 63 64 import static java.time.temporal.ChronoField.EPOCH_DAY; 65 import static java.time.temporal.ChronoUnit.DAYS; 66 import static java.time.temporal.ChronoUnit.FOREVER; 67 68 import java.time.DateTimeException; 69 import java.time.format.ResolverStyle; 70 import java.util.Collections; 71 import java.util.Map; 72 73 /** 74 * A set of date fields that provide access to Julian Days. 75 * <p> 76 * The Julian Day is a standard way of expressing date and time commonly used in the scientific community. 77 * It is expressed as a decimal number of whole days where days start at midday. 78 * This class represents variations on Julian Days that count whole days from midnight. 79 * <p> 80 * The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}. 81 * The fields are supported, and can be queried and set if {@code EPOCH_DAY} is available. 82 * The fields work with all chronologies. 83 * 84 * <h3>Specification for implementors</h3> 85 * This is an immutable and thread-safe class. 86 * 87 * @since 1.8 88 */ 89 public final class JulianFields { 90 91 /** 92 * The offset from Julian to EPOCH DAY. 93 */ 94 private static final long JULIAN_DAY_OFFSET = 2440588L; 95 96 /** 97 * Julian Day field. 98 * <p> 99 * This is an integer-based version of the Julian Day Number. 100 * Julian Day is a well-known system that represents the count of whole days since day 0, 101 * which is defined to be January 1, 4713 BCE in the Julian calendar, and -4713-11-24 Gregorian. 102 * The field has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'. 103 * The field always refers to the local date-time, ignoring the offset or zone. 104 * <p> 105 * For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from 106 * midnight until just before the next midnight. 107 * When 'JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. 108 * 'JULIAN_DAY.adjustInto()' and 'JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects that 109 * can be converted into {@link ChronoField#EPOCH_DAY}. 110 * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object. 111 * <p> 112 * In the resolving phase of parsing, a date can be created from a Julian Day field. 113 * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode} 114 * the Julian Day value is validated against the range of valid values. 115 * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs. 116 * <p> 117 * <h3>Astronomical and Scientific Notes</h3> 118 * The standard astronomical definition uses a fraction to indicate the time-of-day, 119 * thus 3.25 would represent the time 18:00, since days start at midday. 120 * This implementation uses an integer and days starting at midnight. 121 * The integer value for the Julian Day Number is the astronomical Julian Day value at midday 122 * of the date in question. 123 * This amounts to the astronomical Julian Day, rounded to an integer {@code JDN = floor(JD + 0.5)}. 124 * <p> 125 * <pre> 126 * | ISO date | Julian Day Number | Astronomical Julian Day | 127 * | 1970-01-01T00:00 | 2,440,588 | 2,440,587.5 | 128 * | 1970-01-01T06:00 | 2,440,588 | 2,440,587.75 | 129 * | 1970-01-01T12:00 | 2,440,588 | 2,440,588.0 | 130 * | 1970-01-01T18:00 | 2,440,588 | 2,440,588.25 | 131 * | 1970-01-02T00:00 | 2,440,589 | 2,440,588.5 | 132 * | 1970-01-02T06:00 | 2,440,589 | 2,440,588.75 | 133 * | 1970-01-02T12:00 | 2,440,589 | 2,440,589.0 | 134 * </pre> 135 * <p> 136 * Julian Days are sometimes taken to imply Universal Time or UTC, but this 137 * implementation always uses the Julian Day number for the local date, 138 * regardless of the offset or time-zone. 139 */ 140 public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY; 141 142 /** 143 * Modified Julian Day field. 144 * <p> 145 * This is an integer-based version of the Modified Julian Day Number. 146 * Modified Julian Day (MJD) is a well-known system that counts days continuously. 147 * It is defined relative to astronomical Julian Day as {@code MJD = JD - 2400000.5}. 148 * Each Modified Julian Day runs from midnight to midnight. 149 * The field always refers to the local date-time, ignoring the offset or zone. 150 * <p> 151 * For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from 152 * midnight until just before the next midnight. 153 * When 'MODIFIED_JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. 154 * 'MODIFIED_JULIAN_DAY.adjustInto()' and 'MODIFIED_JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects 155 * that can be converted into {@link ChronoField#EPOCH_DAY}. 156 * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object. 157 * <p> 158 * This implementation is an integer version of MJD with the decimal part rounded to floor. 159 * <p> 160 * In the resolving phase of parsing, a date can be created from a Modified Julian Day field. 161 * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode} 162 * the Modified Julian Day value is validated against the range of valid values. 163 * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs. 164 * <p> 165 * <h3>Astronomical and Scientific Notes</h3> 166 * <pre> 167 * | ISO date | Modified Julian Day | Decimal MJD | 168 * | 1970-01-01T00:00 | 40,587 | 40,587.0 | 169 * | 1970-01-01T06:00 | 40,587 | 40,587.25 | 170 * | 1970-01-01T12:00 | 40,587 | 40,587.5 | 171 * | 1970-01-01T18:00 | 40,587 | 40,587.75 | 172 * | 1970-01-02T00:00 | 40,588 | 40,588.0 | 173 * | 1970-01-02T06:00 | 40,588 | 40,588.25 | 174 * | 1970-01-02T12:00 | 40,588 | 40,588.5 | 175 * </pre> 176 * <p> 177 * Modified Julian Days are sometimes taken to imply Universal Time or UTC, but this 178 * implementation always uses the Modified Julian Day for the local date, 179 * regardless of the offset or time-zone. 180 */ 181 public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY; 182 183 /** 184 * Rata Die field. 185 * <p> 186 * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO). 187 * The field always refers to the local date-time, ignoring the offset or zone. 188 * <p> 189 * For date-times, 'RATA_DIE.getFrom()' assumes the same value from 190 * midnight until just before the next midnight. 191 * When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. 192 * 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects 193 * that can be converted into {@link ChronoField#EPOCH_DAY}. 194 * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object. 195 * <p> 196 * In the resolving phase of parsing, a date can be created from a Rata Die field. 197 * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode} 198 * the Rata Die value is validated against the range of valid values. 199 * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs. 200 */ 201 public static final TemporalField RATA_DIE = Field.RATA_DIE; 202 203 /** 204 * Restricted constructor. 205 */ 206 private JulianFields() { 207 throw new AssertionError("Not instantiable"); 208 } 209 210 /** 211 * Implementation of JulianFields. Each instance is a singleton. 212 */ 213 private static enum Field implements TemporalField { 214 JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET), 215 MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L), 216 RATA_DIE("RataDie", DAYS, FOREVER, 719163L); 217 218 private static final long serialVersionUID = -7501623920830201812L; 219 231 this.offset = offset; 232 } 233 234 //----------------------------------------------------------------------- 235 @Override 236 public String getName() { 237 return name; 238 } 239 240 @Override 241 public TemporalUnit getBaseUnit() { 242 return baseUnit; 243 } 244 245 @Override 246 public TemporalUnit getRangeUnit() { 247 return rangeUnit; 248 } 249 250 @Override 251 public boolean isDateBased() { 252 return true; 253 } 254 255 @Override 256 public ValueRange range() { 257 return range; 258 } 259 260 //----------------------------------------------------------------------- 261 @Override 262 public boolean isSupportedBy(TemporalAccessor temporal) { 263 return temporal.isSupported(EPOCH_DAY); 264 } 265 266 @Override 267 public ValueRange rangeRefinedBy(TemporalAccessor temporal) { 268 if (isSupportedBy(temporal) == false) { 269 throw new DateTimeException("Unsupported field: " + this); 270 } 271 return range(); 272 } 273 274 @Override 275 public long getFrom(TemporalAccessor temporal) { 276 return temporal.getLong(EPOCH_DAY) + offset; 277 } 278 279 @SuppressWarnings("unchecked") 280 @Override 281 public <R extends Temporal> R adjustInto(R temporal, long newValue) { 282 if (range().isValidValue(newValue) == false) { 283 throw new DateTimeException("Invalid value: " + name + " " + newValue); 284 } 285 return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset)); 286 } 287 288 //----------------------------------------------------------------------- 289 @Override 290 public Map<TemporalField, Long> resolve(TemporalAccessor temporal, long value, ResolverStyle resolverStyle) { 291 long epochDay; 292 if (resolverStyle == ResolverStyle.LENIENT) { 293 epochDay = Math.subtractExact(value, offset); 294 } else { 295 range().checkValidValue(value, this); 296 epochDay = value - offset; 297 } 298 return Collections.<TemporalField, Long>singletonMap(EPOCH_DAY, epochDay); 299 } 300 301 //----------------------------------------------------------------------- 302 @Override 303 public String toString() { 304 return name; 305 } 306 } 307 } |