jdk/src/share/classes/java/time/temporal/JulianFields.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
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;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    69
import java.util.Collections;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    70
import java.util.Map;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
 * A set of date fields that provide access to Julian Days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
 * 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
    76
 * 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
    77
 * 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
    78
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    79
 * The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    80
 * 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
    81
 * The fields work with all chronologies.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 * This is an immutable and thread-safe class.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
public final class JulianFields {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
     * The offset from Julian to EPOCH DAY.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
    private static final long JULIAN_DAY_OFFSET = 2440588L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
     * Julian Day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
     * This is an integer-based version of the Julian Day Number.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
     * 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
   100
     * 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
   101
     * The field  has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
     * 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
   103
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   104
     * For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
     * midnight until just before the next midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   106
     * 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
   107
     * '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
   108
     * can be converted into {@link ChronoField#EPOCH_DAY}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
     * A {@link DateTimeException} is thrown for any other type of object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
     * <h3>Astronomical and Scientific Notes</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
     * The standard astronomical definition uses a fraction to indicate the time-of-day,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
     * 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
   114
     * This implementation uses an integer and days starting at midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
     * 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
   116
     * of the date in question.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
     * This amounts to the astronomical Julian Day, rounded to an integer {@code JDN = floor(JD + 0.5)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     *  | ISO date          |  Julian Day Number | Astronomical Julian Day |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     *  | 1970-01-01T00:00  |         2,440,588  |         2,440,587.5     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     *  | 1970-01-01T06:00  |         2,440,588  |         2,440,587.75    |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     *  | 1970-01-01T12:00  |         2,440,588  |         2,440,588.0     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     *  | 1970-01-01T18:00  |         2,440,588  |         2,440,588.25    |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
     *  | 1970-01-02T00:00  |         2,440,589  |         2,440,588.5     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
     *  | 1970-01-02T06:00  |         2,440,589  |         2,440,588.75    |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     *  | 1970-01-02T12:00  |         2,440,589  |         2,440,589.0     |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     * Julian Days are sometimes taken to imply Universal Time or UTC, but this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * implementation always uses the Julian Day number for the local date,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * regardless of the offset or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   134
    public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * Modified Julian Day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * This is an integer-based version of the Modified Julian Day Number.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     * Modified Julian Day (MJD) is a well-known system that counts days continuously.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * 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
   142
     * Each Modified Julian Day runs from midnight to midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     * 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
   144
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   145
     * For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * midnight until just before the next midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   147
     * 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
   148
     * '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
   149
     * that can be converted into {@link ChronoField#EPOCH_DAY}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     * A {@link DateTimeException} is thrown for any other type of object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
     * 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
   153
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     * <h3>Astronomical and Scientific Notes</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     *  | ISO date          | Modified Julian Day |      Decimal MJD |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     *  | 1970-01-01T00:00  |             40,587  |       40,587.0   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     *  | 1970-01-01T06:00  |             40,587  |       40,587.25  |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     *  | 1970-01-01T12:00  |             40,587  |       40,587.5   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     *  | 1970-01-01T18:00  |             40,587  |       40,587.75  |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     *  | 1970-01-02T00:00  |             40,588  |       40,588.0   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     *  | 1970-01-02T06:00  |             40,588  |       40,588.25  |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     *  | 1970-01-02T12:00  |             40,588  |       40,588.5   |
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * 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
   167
     * implementation always uses the Modified Julian Day for the local date,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     * regardless of the offset or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   170
    public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     * Rata Die field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     * 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
   176
     * 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
   177
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   178
     * For date-times, 'RATA_DIE.getFrom()' assumes the same value from
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * midnight until just before the next midnight.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   180
     * 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
   181
     * '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
   182
     * that can be converted into {@link ChronoField#EPOCH_DAY}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * A {@link DateTimeException} is thrown for any other type of object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   185
    public static final TemporalField RATA_DIE = Field.RATA_DIE;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     * Restricted constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
    private JulianFields() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
        throw new AssertionError("Not instantiable");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   195
     * Implementation of JulianFields.  Each instance is a singleton.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   197
    private static enum Field implements TemporalField {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   198
        JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   199
        MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   200
        RATA_DIE("RataDie", DAYS, FOREVER, 719163L);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
        private static final long serialVersionUID = -7501623920830201812L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   204
        private final transient String name;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
        private final transient TemporalUnit baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
        private final transient TemporalUnit rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
        private final transient ValueRange range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        private final transient long offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
        private Field(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, long offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
            this.name = name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
            this.baseUnit = baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
            this.rangeUnit = rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
            this.range = ValueRange.of(-365243219162L + offset, 365241780471L + offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
            this.offset = offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
        public String getName() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
            return name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
        public TemporalUnit getBaseUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
            return baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
        public TemporalUnit getRangeUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
            return rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
        public ValueRange range() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
            return range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   241
        public boolean isSupportedBy(TemporalAccessor temporal) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
            return temporal.isSupported(EPOCH_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   246
        public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   247
            if (isSupportedBy(temporal) == false) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
                throw new DateTimeException("Unsupported field: " + this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
            return range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   254
        public long getFrom(TemporalAccessor temporal) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
            return temporal.getLong(EPOCH_DAY) + offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
        @SuppressWarnings("unchecked")
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   260
        public <R extends Temporal> R adjustInto(R temporal, long newValue) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
            if (range().isValidValue(newValue) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
                throw new DateTimeException("Invalid value: " + name + " " + newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
            return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset));
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
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   269
        public Map<TemporalField, Long> resolve(TemporalAccessor temporal, long value) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   270
            return Collections.<TemporalField, Long>singletonMap(EPOCH_DAY, Math.subtractExact(value, offset));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
        public String toString() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
            return name;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
}