jdk/src/share/classes/java/time/temporal/JulianFields.java
author rriggs
Thu, 17 Oct 2013 10:37:23 -0400
changeset 20873 e91d5b1cb8e6
parent 20793 5acd4b8d70a1
permissions -rw-r--r--
8026516: javadoc errors in java.time Summary: Corrected links to TemporalQuery and TemporalField.resolve Reviewed-by: mduigou, darcy, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time.temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.EPOCH_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoUnit.DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoUnit.FOREVER;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.time.DateTimeException;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    69
import java.time.chrono.ChronoLocalDate;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    70
import java.time.chrono.Chronology;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    71
import java.time.format.ResolverStyle;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    72
import java.util.Map;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
 * A set of date fields that provide access to Julian Days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
 * The Julian Day is a standard way of expressing date and time commonly used in the scientific community.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
 * It is expressed as a decimal number of whole days where days start at midday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
 * This class represents variations on Julian Days that count whole days from midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    80
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    81
 * The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    82
 * The fields are supported, and can be queried and set if {@code EPOCH_DAY} is available.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    83
 * The fields work with all chronologies.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
    85
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
 * This is an immutable and thread-safe class.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
public final class JulianFields {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
     * The offset from Julian to EPOCH DAY.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
    private static final long JULIAN_DAY_OFFSET = 2440588L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
     * Julian Day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
     * This is an integer-based version of the Julian Day Number.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
     * Julian Day is a well-known system that represents the count of whole days since day 0,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
     * which is defined to be January 1, 4713 BCE in the Julian calendar, and -4713-11-24 Gregorian.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
     * The field  has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
     * The field always refers to the local date-time, ignoring the offset or zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   106
     * For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
     * midnight until just before the next midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   108
     * When 'JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   109
     * 'JULIAN_DAY.adjustInto()' and 'JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects that
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
     * can be converted into {@link ChronoField#EPOCH_DAY}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   111
     * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   112
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   113
     * In the resolving phase of parsing, a date can be created from a Julian Day field.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   114
     * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   115
     * the Julian Day value is validated against the range of valid values.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   116
     * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   117
     *
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     * <h3>Astronomical and Scientific Notes</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     * The standard astronomical definition uses a fraction to indicate the time-of-day,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     * thus 3.25 would represent the time 18:00, since days start at midday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     * This implementation uses an integer and days starting at midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * The integer value for the Julian Day Number is the astronomical Julian Day value at midday
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * of the date in question.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     * This amounts to the astronomical Julian Day, rounded to an integer {@code JDN = floor(JD + 0.5)}.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   125
     *
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     *  | ISO date          |  Julian Day Number | Astronomical Julian Day |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     *  | 1970-01-01T00:00  |         2,440,588  |         2,440,587.5     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     *  | 1970-01-01T06:00  |         2,440,588  |         2,440,587.75    |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     *  | 1970-01-01T12:00  |         2,440,588  |         2,440,588.0     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     *  | 1970-01-01T18:00  |         2,440,588  |         2,440,588.25    |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     *  | 1970-01-02T00:00  |         2,440,589  |         2,440,588.5     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     *  | 1970-01-02T06:00  |         2,440,589  |         2,440,588.75    |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     *  | 1970-01-02T12:00  |         2,440,589  |         2,440,589.0     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * Julian Days are sometimes taken to imply Universal Time or UTC, but this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * implementation always uses the Julian Day number for the local date,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * regardless of the offset or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   141
    public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
     * Modified Julian Day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * This is an integer-based version of the Modified Julian Day Number.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * Modified Julian Day (MJD) is a well-known system that counts days continuously.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     * It is defined relative to astronomical Julian Day as  {@code MJD = JD - 2400000.5}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
     * Each Modified Julian Day runs from midnight to midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     * The field always refers to the local date-time, ignoring the offset or zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   152
     * For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     * midnight until just before the next midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   154
     * When 'MODIFIED_JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   155
     * 'MODIFIED_JULIAN_DAY.adjustInto()' and 'MODIFIED_JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * that can be converted into {@link ChronoField#EPOCH_DAY}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   157
     * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     * This implementation is an integer version of MJD with the decimal part rounded to floor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   161
     * In the resolving phase of parsing, a date can be created from a Modified Julian Day field.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   162
     * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   163
     * the Modified Julian Day value is validated against the range of valid values.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   164
     * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   165
     *
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * <h3>Astronomical and Scientific Notes</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     *  | ISO date          | Modified Julian Day |      Decimal MJD |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     *  | 1970-01-01T00:00  |             40,587  |       40,587.0   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     *  | 1970-01-01T06:00  |             40,587  |       40,587.25  |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     *  | 1970-01-01T12:00  |             40,587  |       40,587.5   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     *  | 1970-01-01T18:00  |             40,587  |       40,587.75  |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     *  | 1970-01-02T00:00  |             40,588  |       40,588.0   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
     *  | 1970-01-02T06:00  |             40,588  |       40,588.25  |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     *  | 1970-01-02T12:00  |             40,588  |       40,588.5   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * </pre>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   177
     *
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * Modified Julian Days are sometimes taken to imply Universal Time or UTC, but this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * implementation always uses the Modified Julian Day for the local date,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * regardless of the offset or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   182
    public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * Rata Die field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
     * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     * The field always refers to the local date-time, ignoring the offset or zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   190
     * For date-times, 'RATA_DIE.getFrom()' assumes the same value from
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     * midnight until just before the next midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
     * When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   193
     * 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
     * that can be converted into {@link ChronoField#EPOCH_DAY}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   195
     * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   196
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   197
     * In the resolving phase of parsing, a date can be created from a Rata Die field.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   198
     * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   199
     * the Rata Die value is validated against the range of valid values.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   200
     * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   202
    public static final TemporalField RATA_DIE = Field.RATA_DIE;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * Restricted constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    private JulianFields() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        throw new AssertionError("Not instantiable");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   212
     * Implementation of JulianFields.  Each instance is a singleton.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   214
    private static enum Field implements TemporalField {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   215
        JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   216
        MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   217
        RATA_DIE("RataDie", DAYS, FOREVER, 719163L);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
        private static final long serialVersionUID = -7501623920830201812L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   221
        private final transient String name;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        private final transient TemporalUnit baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
        private final transient TemporalUnit rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
        private final transient ValueRange range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
        private final transient long offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
        private Field(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, long offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
            this.name = name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
            this.baseUnit = baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
            this.rangeUnit = rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
            this.range = ValueRange.of(-365243219162L + offset, 365241780471L + offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
            this.offset = offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
        public TemporalUnit getBaseUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
            return baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
        public TemporalUnit getRangeUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
            return rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
        @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   247
        public boolean isDateBased() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   248
            return true;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   249
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   250
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   251
        @Override
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   252
        public boolean isTimeBased() {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   253
            return false;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   254
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   255
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   256
        @Override
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
        public ValueRange range() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
            return range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   263
        public boolean isSupportedBy(TemporalAccessor temporal) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
            return temporal.isSupported(EPOCH_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   268
        public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   269
            if (isSupportedBy(temporal) == false) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
                throw new DateTimeException("Unsupported field: " + this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
            return range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
        public long getFrom(TemporalAccessor temporal) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
            return temporal.getLong(EPOCH_DAY) + offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   280
        @SuppressWarnings("unchecked")
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
        public <R extends Temporal> R adjustInto(R temporal, long newValue) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
            if (range().isValidValue(newValue) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
                throw new DateTimeException("Invalid value: " + name + " " + newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
            return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
        @Override
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   291
        public ChronoLocalDate resolve(
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   292
                Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   293
            long value = fieldValues.remove(this);
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   294
            Chronology chrono = Chronology.from(partialTemporal);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   295
            if (resolverStyle == ResolverStyle.LENIENT) {
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   296
                return chrono.dateEpochDay(Math.subtractExact(value, offset));
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   297
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   298
            range().checkValidValue(value, this);
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   299
            return chrono.dateEpochDay(value - offset);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
        public String toString() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   305
            return name;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
}