jdk/src/java.base/share/classes/java/time/chrono/Chronology.java
author rriggs
Fri, 06 Feb 2015 17:29:11 -0500
changeset 28848 824124d1cba5
parent 25859 3317bb8137f4
child 33675 7d9d372a41df
permissions -rw-r--r--
8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear Summary: Check the year range consistently with other Hijrah date years Reviewed-by: lancea, scolebourne
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     1
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     4
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    10
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    15
 * accompanied this code).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    16
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    20
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    23
 * questions.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    24
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    25
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    26
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    30
 * file:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    31
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    32
 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    33
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    34
 * All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    35
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    38
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    41
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    45
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    49
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    61
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    62
package java.time.chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    63
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    64
import java.time.Clock;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    65
import java.time.DateTimeException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    66
import java.time.Instant;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    67
import java.time.LocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    68
import java.time.LocalTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    69
import java.time.ZoneId;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    70
import java.time.format.DateTimeFormatterBuilder;
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:
diff changeset
    72
import java.time.format.TextStyle;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    73
import java.time.temporal.ChronoField;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    74
import java.time.temporal.TemporalAccessor;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    75
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
    76
import java.time.temporal.TemporalQueries;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    77
import java.time.temporal.TemporalQuery;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    78
import java.time.temporal.UnsupportedTemporalTypeException;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    79
import java.time.temporal.ValueRange;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    80
import java.util.List;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    81
import java.util.Locale;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    82
import java.util.Map;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    83
import java.util.Objects;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    84
import java.util.Set;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    85
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    86
/**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    87
 * A calendar system, used to organize and identify dates.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    88
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    89
 * The main date and time API is built on the ISO calendar system.
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
    90
 * The chronology operates behind the scenes to represent the general concept of a calendar system.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    91
 * For example, the Japanese, Minguo, Thai Buddhist and others.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    92
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    93
 * Most other calendar systems also operate on the shared concepts of year, month and day,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    94
 * linked to the cycles of the Earth around the Sun, and the Moon around the Earth.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    95
 * These shared concepts are defined by {@link ChronoField} and are available
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    96
 * for use by any {@code Chronology} implementation:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    97
 * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    98
 *   LocalDate isoDate = ...
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    99
 *   ThaiBuddhistDate thaiDate = ...
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   100
 *   int isoYear = isoDate.get(ChronoField.YEAR);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   101
 *   int thaiYear = thaiDate.get(ChronoField.YEAR);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   102
 * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   103
 * As shown, although the date objects are in different calendar systems, represented by different
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   104
 * {@code Chronology} instances, both can be queried using the same constant on {@code ChronoField}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   105
 * For a full discussion of the implications of this, see {@link ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   106
 * In general, the advice is to use the known ISO-based {@code LocalDate}, rather than
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   107
 * {@code ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   108
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   109
 * While a {@code Chronology} object typically uses {@code ChronoField} and is based on
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   110
 * an era, year-of-era, month-of-year, day-of-month model of a date, this is not required.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   111
 * A {@code Chronology} instance may represent a totally different kind of calendar system,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   112
 * such as the Mayan.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   113
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   114
 * In practical terms, the {@code Chronology} instance also acts as a factory.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   115
 * The {@link #of(String)} method allows an instance to be looked up by identifier,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   116
 * while the {@link #ofLocale(Locale)} method allows lookup by locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   117
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   118
 * The {@code Chronology} instance provides a set of methods to create {@code ChronoLocalDate} instances.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   119
 * The date classes are used to manipulate specific dates.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   120
 * <ul>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   121
 * <li> {@link #dateNow() dateNow()}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   122
 * <li> {@link #dateNow(Clock) dateNow(clock)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   123
 * <li> {@link #dateNow(ZoneId) dateNow(zone)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   124
 * <li> {@link #date(int, int, int) date(yearProleptic, month, day)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   125
 * <li> {@link #date(Era, int, int, int) date(era, yearOfEra, month, day)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   126
 * <li> {@link #dateYearDay(int, int) dateYearDay(yearProleptic, dayOfYear)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   127
 * <li> {@link #dateYearDay(Era, int, int) dateYearDay(era, yearOfEra, dayOfYear)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   128
 * <li> {@link #date(TemporalAccessor) date(TemporalAccessor)}
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   129
 * </ul>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   130
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   131
 * <h3 id="addcalendars">Adding New Calendars</h3>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   132
 * The set of available chronologies can be extended by applications.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   133
 * Adding a new calendar system requires the writing of an implementation of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   134
 * {@code Chronology}, {@code ChronoLocalDate} and {@code Era}.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20873
diff changeset
   135
 * The majority of the logic specific to the calendar system will be in the
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20873
diff changeset
   136
 * {@code ChronoLocalDate} implementation.
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20873
diff changeset
   137
 * The {@code Chronology} implementation acts as a factory.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   138
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   139
 * To permit the discovery of additional chronologies, the {@link java.util.ServiceLoader ServiceLoader}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   140
 * is used. A file must be added to the {@code META-INF/services} directory with the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   141
 * name 'java.time.chrono.Chronology' listing the implementation classes.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   142
 * See the ServiceLoader for more details on service loading.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   143
 * For lookup by id or calendarType, the system provided calendars are found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   144
 * first followed by application provided calendars.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   145
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   146
 * Each chronology must define a chronology ID that is unique within the system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   147
 * If the chronology represents a calendar system defined by the
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   148
 * CLDR specification then the calendar type is the concatenation of the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   149
 * CLDR type and, if applicable, the CLDR variant,
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   150
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   151
 * @implSpec
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   152
 * This interface must be implemented with care to ensure other classes operate correctly.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   153
 * All implementations that can be instantiated must be final, immutable and thread-safe.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   154
 * Subclasses should be Serializable wherever possible.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   155
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   156
 * @since 1.8
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   157
 */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   158
public interface Chronology extends Comparable<Chronology> {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   159
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   160
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   161
     * Obtains an instance of {@code Chronology} from a temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   162
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   163
     * This obtains a chronology based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   164
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   165
     * which this factory converts to an instance of {@code Chronology}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   166
     * <p>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   167
     * The conversion will obtain the chronology using {@link TemporalQueries#chronology()}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   168
     * If the specified temporal object does not have a chronology, {@link IsoChronology} is returned.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   169
     * <p>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   170
     * This method matches the signature of the functional interface {@link TemporalQuery}
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20873
diff changeset
   171
     * allowing it to be used as a query via method reference, {@code Chronology::from}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   172
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   173
     * @param temporal  the temporal to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   174
     * @return the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   175
     * @throws DateTimeException if unable to convert to an {@code Chronology}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   176
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   177
    static Chronology from(TemporalAccessor temporal) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   178
        Objects.requireNonNull(temporal, "temporal");
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   179
        Chronology obj = temporal.query(TemporalQueries.chronology());
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   180
        return (obj != null ? obj : IsoChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   181
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   182
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   183
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   184
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   185
     * Obtains an instance of {@code Chronology} from a locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   186
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   187
     * This returns a {@code Chronology} based on the specified locale,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   188
     * typically returning {@code IsoChronology}. Other calendar systems
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   189
     * are only returned if they are explicitly selected within the locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   190
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   191
     * The {@link Locale} class provide access to a range of information useful
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   192
     * for localizing an application. This includes the language and region,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   193
     * such as "en-GB" for English as used in Great Britain.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   194
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   195
     * The {@code Locale} class also supports an extension mechanism that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   196
     * can be used to identify a calendar system. The mechanism is a form
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   197
     * of key-value pairs, where the calendar system has the key "ca".
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   198
     * For example, the locale "en-JP-u-ca-japanese" represents the English
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   199
     * language as used in Japan with the Japanese calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   200
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   201
     * This method finds the desired calendar system by in a manner equivalent
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   202
     * to passing "ca" to {@link Locale#getUnicodeLocaleType(String)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   203
     * If the "ca" key is not present, then {@code IsoChronology} is returned.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   204
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   205
     * Note that the behavior of this method differs from the older
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   206
     * {@link java.util.Calendar#getInstance(Locale)} method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   207
     * If that method receives a locale of "th_TH" it will return {@code BuddhistCalendar}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   208
     * By contrast, this method will return {@code IsoChronology}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   209
     * Passing the locale "th-TH-u-ca-buddhist" into either method will
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   210
     * result in the Thai Buddhist calendar system and is therefore the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   211
     * recommended approach going forward for Thai calendar system localization.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   212
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   213
     * A similar, but simpler, situation occurs for the Japanese calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   214
     * The locale "jp_JP_JP" has previously been used to access the calendar.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   215
     * However, unlike the Thai locale, "ja_JP_JP" is automatically converted by
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   216
     * {@code Locale} to the modern and recommended form of "ja-JP-u-ca-japanese".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   217
     * Thus, there is no difference in behavior between this method and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   218
     * {@code Calendar#getInstance(Locale)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   219
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   220
     * @param locale  the locale to use to obtain the calendar system, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   221
     * @return the calendar system associated with the locale, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   222
     * @throws DateTimeException if the locale-specified calendar cannot be found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   223
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   224
    static Chronology ofLocale(Locale locale) {
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   225
        return AbstractChronology.ofLocale(locale);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   226
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   227
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   228
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   229
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   230
     * Obtains an instance of {@code Chronology} from a chronology ID or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   231
     * calendar system type.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   232
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   233
     * This returns a chronology based on either the ID or the type.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   234
     * The {@link #getId() chronology ID} uniquely identifies the chronology.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   235
     * The {@link #getCalendarType() calendar system type} is defined by the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   236
     * CLDR specification.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   237
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   238
     * The chronology may be a system chronology or a chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   239
     * provided by the application via ServiceLoader configuration.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   240
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   241
     * Since some calendars can be customized, the ID or type typically refers
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   242
     * to the default customization. For example, the Gregorian calendar can have multiple
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   243
     * cutover dates from the Julian, but the lookup only provides the default cutover date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   244
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   245
     * @param id  the chronology ID or calendar system type, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   246
     * @return the chronology with the identifier requested, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   247
     * @throws DateTimeException if the chronology cannot be found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   248
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   249
    static Chronology of(String id) {
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   250
        return AbstractChronology.of(id);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   251
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   252
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   253
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   254
     * Returns the available chronologies.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   255
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   256
     * Each returned {@code Chronology} is available for use in the system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   257
     * The set of chronologies includes the system chronologies and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   258
     * any chronologies provided by the application via ServiceLoader
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   259
     * configuration.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   260
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   261
     * @return the independent, modifiable set of the available chronology IDs, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   262
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   263
    static Set<Chronology> getAvailableChronologies() {
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   264
        return AbstractChronology.getAvailableChronologies();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   265
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   266
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   267
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   268
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   269
     * Gets the ID of the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   270
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   271
     * The ID uniquely identifies the {@code Chronology}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   272
     * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   273
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   274
     * @return the chronology ID, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   275
     * @see #getCalendarType()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   276
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   277
    String getId();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   278
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   279
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   280
     * Gets the calendar type of the calendar system.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   281
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   282
     * The calendar type is an identifier defined by the CLDR and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   283
     * <em>Unicode Locale Data Markup Language (LDML)</em> specifications
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   284
     * to uniquely identification a calendar.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   285
     * The {@code getCalendarType} is the concatenation of the CLDR calendar type
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   286
     * and the variant, if applicable, is appended separated by "-".
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   287
     * The calendar type is used to lookup the {@code Chronology} using {@link #of(String)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   288
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   289
     * @return the calendar system type, null if the calendar is not defined by CLDR/LDML
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   290
     * @see #getId()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   291
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   292
    String getCalendarType();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   293
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   294
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   295
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   296
     * Obtains a local date in this chronology from the era, year-of-era,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   297
     * month-of-year and day-of-month fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   298
     *
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   299
     * @implSpec
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   300
     * The default implementation combines the era and year-of-era into a proleptic
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   301
     * year before calling {@link #date(int, int, int)}.
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   302
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   303
     * @param era  the era of the correct type for the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   304
     * @param yearOfEra  the chronology year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   305
     * @param month  the chronology month-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   306
     * @param dayOfMonth  the chronology day-of-month
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   307
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   308
     * @throws DateTimeException if unable to create the date
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   309
     * @throws ClassCastException if the {@code era} is not of the correct type for the chronology
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   310
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   311
    default ChronoLocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   312
        return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   313
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   314
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   315
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   316
     * Obtains a local date in this chronology from the proleptic-year,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   317
     * month-of-year and day-of-month fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   318
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   319
     * @param prolepticYear  the chronology proleptic-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   320
     * @param month  the chronology month-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   321
     * @param dayOfMonth  the chronology day-of-month
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   322
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   323
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   324
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   325
    ChronoLocalDate date(int prolepticYear, int month, int dayOfMonth);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   326
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   327
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   328
     * Obtains a local date in this chronology from the era, year-of-era and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   329
     * day-of-year fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   330
     *
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   331
     * @implSpec
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   332
     * The default implementation combines the era and year-of-era into a proleptic
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   333
     * year before calling {@link #dateYearDay(int, int)}.
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   334
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   335
     * @param era  the era of the correct type for the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   336
     * @param yearOfEra  the chronology year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   337
     * @param dayOfYear  the chronology day-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   338
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   339
     * @throws DateTimeException if unable to create the date
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   340
     * @throws ClassCastException if the {@code era} is not of the correct type for the chronology
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   341
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   342
    default ChronoLocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   343
        return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   344
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   345
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   346
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   347
     * Obtains a local date in this chronology from the proleptic-year and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   348
     * day-of-year fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   349
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   350
     * @param prolepticYear  the chronology proleptic-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   351
     * @param dayOfYear  the chronology day-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   352
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   353
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   354
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   355
    ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   356
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   357
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   358
     * Obtains a local date in this chronology from the epoch-day.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   359
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   360
     * The definition of {@link ChronoField#EPOCH_DAY EPOCH_DAY} is the same
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   361
     * for all calendar systems, thus it can be used for conversion.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   362
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   363
     * @param epochDay  the epoch day
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   364
     * @return the local date in this chronology, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   365
     * @throws DateTimeException if unable to create the date
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   366
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   367
    ChronoLocalDate dateEpochDay(long epochDay);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   368
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   369
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   370
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   371
     * Obtains the current local date in this chronology from the system clock in the default time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   372
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   373
     * This will query the {@link Clock#systemDefaultZone() system clock} in the default
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   374
     * time-zone to obtain the current date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   375
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   376
     * Using this method will prevent the ability to use an alternate clock for testing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   377
     * because the clock is hard-coded.
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   378
     *
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   379
     * @implSpec
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   380
     * The default implementation invokes {@link #dateNow(Clock)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   381
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   382
     * @return the current local date using the system clock and default time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   383
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   384
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   385
    default ChronoLocalDate dateNow() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   386
        return dateNow(Clock.systemDefaultZone());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   387
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   388
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   389
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   390
     * Obtains the current local date in this chronology from the system clock in the specified time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   391
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   392
     * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   393
     * Specifying the time-zone avoids dependence on the default time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   394
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   395
     * Using this method will prevent the ability to use an alternate clock for testing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   396
     * because the clock is hard-coded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   397
     *
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   398
     * @implSpec
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   399
     * The default implementation invokes {@link #dateNow(Clock)}.
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   400
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   401
     * @param zone  the zone ID to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   402
     * @return the current local date using the system clock, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   403
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   404
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   405
    default ChronoLocalDate dateNow(ZoneId zone) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   406
        return dateNow(Clock.system(zone));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   407
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   408
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   409
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   410
     * Obtains the current local date in this chronology from the specified clock.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   411
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   412
     * This will query the specified clock to obtain the current date - today.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   413
     * Using this method allows the use of an alternate clock for testing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   414
     * The alternate clock may be introduced using {@link Clock dependency injection}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   415
     *
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   416
     * @implSpec
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20873
diff changeset
   417
     * The default implementation invokes {@link #date(TemporalAccessor)}.
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   418
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   419
     * @param clock  the clock to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   420
     * @return the current local date, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   421
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   422
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   423
    default ChronoLocalDate dateNow(Clock clock) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   424
        Objects.requireNonNull(clock, "clock");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   425
        return date(LocalDate.now(clock));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   426
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   427
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   428
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   429
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   430
     * Obtains a local date in this chronology from another temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   431
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   432
     * This obtains a date in this chronology based on the specified temporal.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   433
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   434
     * which this factory converts to an instance of {@code ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   435
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   436
     * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   437
     * field, which is standardized across calendar systems.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   438
     * <p>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   439
     * This method matches the signature of the functional interface {@link TemporalQuery}
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   440
     * allowing it to be used as a query via method reference, {@code aChronology::date}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   441
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   442
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   443
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   444
     * @throws DateTimeException if unable to create the date
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   445
     * @see ChronoLocalDate#from(TemporalAccessor)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   446
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   447
    ChronoLocalDate date(TemporalAccessor temporal);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   448
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   449
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   450
     * Obtains a local date-time in this chronology from another temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   451
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   452
     * This obtains a date-time in this chronology based on the specified temporal.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   453
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   454
     * which this factory converts to an instance of {@code ChronoLocalDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   455
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   456
     * The conversion extracts and combines the {@code ChronoLocalDate} and the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   457
     * {@code LocalTime} from the temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   458
     * Implementations are permitted to perform optimizations such as accessing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   459
     * those fields that are equivalent to the relevant objects.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   460
     * The result uses this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   461
     * <p>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   462
     * This method matches the signature of the functional interface {@link TemporalQuery}
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   463
     * allowing it to be used as a query via method reference, {@code aChronology::localDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   464
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   465
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   466
     * @return the local date-time in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   467
     * @throws DateTimeException if unable to create the date-time
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   468
     * @see ChronoLocalDateTime#from(TemporalAccessor)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   469
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   470
    default ChronoLocalDateTime<? extends ChronoLocalDate> localDateTime(TemporalAccessor temporal) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   471
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   472
            return date(temporal).atTime(LocalTime.from(temporal));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   473
        } catch (DateTimeException ex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   474
            throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass(), ex);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   475
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   476
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   477
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   478
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   479
     * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   480
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   481
     * This obtains a zoned date-time in this chronology based on the specified temporal.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   482
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   483
     * which this factory converts to an instance of {@code ChronoZonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   484
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   485
     * The conversion will first obtain a {@code ZoneId} from the temporal object,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   486
     * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   487
     * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   488
     * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   489
     * with {@code Instant} or {@code ChronoLocalDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   490
     * Implementations are permitted to perform optimizations such as accessing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   491
     * those fields that are equivalent to the relevant objects.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   492
     * The result uses this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   493
     * <p>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   494
     * This method matches the signature of the functional interface {@link TemporalQuery}
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   495
     * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   496
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   497
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   498
     * @return the zoned date-time in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   499
     * @throws DateTimeException if unable to create the date-time
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   500
     * @see ChronoZonedDateTime#from(TemporalAccessor)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   501
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   502
    default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   503
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   504
            ZoneId zone = ZoneId.from(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   505
            try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   506
                Instant instant = Instant.from(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   507
                return zonedDateTime(instant, zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   508
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   509
            } catch (DateTimeException ex1) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   510
                ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   511
                return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   512
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   513
        } catch (DateTimeException ex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   514
            throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   515
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   516
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   517
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   518
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   519
     * Obtains a {@code ChronoZonedDateTime} in this chronology from an {@code Instant}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   520
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   521
     * This obtains a zoned date-time with the same instant as that specified.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   522
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   523
     * @param instant  the instant to create the date-time from, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   524
     * @param zone  the time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   525
     * @return the zoned date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   526
     * @throws DateTimeException if the result exceeds the supported range
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   527
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   528
    default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(Instant instant, ZoneId zone) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   529
        return ChronoZonedDateTimeImpl.ofInstant(this, instant, zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   530
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   531
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   532
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   533
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   534
     * Checks if the specified year is a leap year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   535
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   536
     * A leap-year is a year of a longer length than normal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   537
     * The exact meaning is determined by the chronology according to the following constraints.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   538
     * <ul>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   539
     * <li>a leap-year must imply a year-length longer than a non leap-year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   540
     * <li>a chronology that does not support the concept of a year must return false.
28848
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   541
     * <li>the correct result must be returned for all years within the
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   542
     *     valid range of years for the chronology.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   543
     * </ul>
28848
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   544
     * <p>
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   545
     * Outside the range of valid years an implementation is free to return
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   546
     * either a best guess or false.
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   547
     * An implementation must not throw an exception, even if the year is
824124d1cba5 8067800: Unexpected DateTimeException in the java.time.chrono.HijrahChronology.isLeapYear
rriggs
parents: 25859
diff changeset
   548
     * outside the range of valid years.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   549
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   550
     * @param prolepticYear  the proleptic-year to check, not validated for range
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   551
     * @return true if the year is a leap year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   552
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   553
    boolean isLeapYear(long prolepticYear);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   554
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   555
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   556
     * Calculates the proleptic-year given the era and year-of-era.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   557
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   558
     * This combines the era and year-of-era into the single proleptic-year field.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   559
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   560
     * If the chronology makes active use of eras, such as {@code JapaneseChronology}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   561
     * then the year-of-era will be validated against the era.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   562
     * For other chronologies, validation is optional.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   563
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   564
     * @param era  the era of the correct type for the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   565
     * @param yearOfEra  the chronology year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   566
     * @return the proleptic-year
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   567
     * @throws DateTimeException if unable to convert to a proleptic-year,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   568
     *  such as if the year is invalid for the era
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   569
     * @throws ClassCastException if the {@code era} is not of the correct type for the chronology
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   570
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   571
    int prolepticYear(Era era, int yearOfEra);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   572
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   573
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   574
     * Creates the chronology era object from the numeric value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   575
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   576
     * The era is, conceptually, the largest division of the time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   577
     * Most calendar systems have a single epoch dividing the time-line into two eras.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   578
     * However, some have multiple eras, such as one for the reign of each leader.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   579
     * The exact meaning is determined by the chronology according to the following constraints.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   580
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   581
     * The era in use at 1970-01-01 must have the value 1.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   582
     * Later eras must have sequentially higher values.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   583
     * Earlier eras must have sequentially lower values.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   584
     * Each chronology must refer to an enum or similar singleton to provide the era values.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   585
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   586
     * This method returns the singleton era of the correct type for the specified era value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   587
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   588
     * @param eraValue  the era value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   589
     * @return the calendar system era, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   590
     * @throws DateTimeException if unable to create the era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   591
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   592
    Era eraOf(int eraValue);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   593
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   594
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   595
     * Gets the list of eras for the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   596
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   597
     * Most calendar systems have an era, within which the year has meaning.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   598
     * If the calendar system does not support the concept of eras, an empty
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   599
     * list must be returned.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   600
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   601
     * @return the list of eras for the chronology, may be immutable, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   602
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   603
    List<Era> eras();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   604
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   605
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   606
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   607
     * Gets the range of valid values for the specified field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   608
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   609
     * All fields can be expressed as a {@code long} integer.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   610
     * This method returns an object that describes the valid range for that value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   611
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   612
     * Note that the result only describes the minimum and maximum valid values
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   613
     * and it is important not to read too much into them. For example, there
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   614
     * could be values within the range that are invalid for the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   615
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   616
     * This method will return a result whether or not the chronology supports the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   617
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   618
     * @param field  the field to get the range for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   619
     * @return the range of valid values for the field, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   620
     * @throws DateTimeException if the range for the field cannot be obtained
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   621
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   622
    ValueRange range(ChronoField field);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   623
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   624
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   625
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   626
     * Gets the textual representation of this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   627
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   628
     * This returns the textual name used to identify the chronology,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   629
     * suitable for presentation to the user.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   630
     * The parameters control the style of the returned text and the locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   631
     *
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   632
     * @implSpec
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20873
diff changeset
   633
     * The default implementation behaves as though the formatter was used to
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   634
     * format the chronology textual name.
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   635
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   636
     * @param style  the style of the text required, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   637
     * @param locale  the locale to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   638
     * @return the text value of the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   639
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   640
    default String getDisplayName(TextStyle style, Locale locale) {
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   641
        TemporalAccessor temporal = new TemporalAccessor() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   642
            @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   643
            public boolean isSupported(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   644
                return false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   645
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   646
            @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   647
            public long getLong(TemporalField field) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   648
                throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   649
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   650
            @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   651
            @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   652
            public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   653
                if (query == TemporalQueries.chronology()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   654
                    return (R) Chronology.this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   655
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   656
                return TemporalAccessor.super.query(query);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   657
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   658
        };
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   659
        return new DateTimeFormatterBuilder().appendChronologyText(style).toFormatter(locale).format(temporal);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   660
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   661
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   662
    //-----------------------------------------------------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   663
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   664
     * Resolves parsed {@code ChronoField} values into a date during parsing.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   665
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   666
     * Most {@code TemporalField} implementations are resolved using the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   667
     * resolve method on the field. By contrast, the {@code ChronoField} class
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   668
     * defines fields that only have meaning relative to the chronology.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   669
     * As such, {@code ChronoField} date fields are resolved here in the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   670
     * context of a specific chronology.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   671
     * <p>
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   672
     * The default implementation, which explains typical resolve behaviour,
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   673
     * is provided in {@link AbstractChronology}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   674
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   675
     * @param fieldValues  the map of fields to values, which can be updated, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   676
     * @param resolverStyle  the requested type of resolve, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   677
     * @return the resolved date, null if insufficient information to create a date
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   678
     * @throws DateTimeException if the date cannot be resolved, typically
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   679
     *  because of a conflict in the input data
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   680
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   681
    ChronoLocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   682
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   683
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   684
    /**
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   685
     * Obtains a period for this chronology based on years, months and days.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   686
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   687
     * This returns a period tied to this chronology using the specified
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   688
     * years, months and days.  All supplied chronologies use periods
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   689
     * based on years, months and days, however the {@code ChronoPeriod} API
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   690
     * allows the period to be represented using other units.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   691
     *
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   692
     * @implSpec
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   693
     * The default implementation returns an implementation class suitable
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   694
     * for most calendar systems. It is based solely on the three units.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   695
     * Normalization, addition and subtraction derive the number of months
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   696
     * in a year from the {@link #range(ChronoField)}. If the number of
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   697
     * months within a year is fixed, then the calculation approach for
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   698
     * addition, subtraction and normalization is slightly different.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   699
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   700
     * If implementing an unusual calendar system that is not based on
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   701
     * years, months and days, or where you want direct control, then
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   702
     * the {@code ChronoPeriod} interface must be directly implemented.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   703
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   704
     * The returned period is immutable and thread-safe.
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   705
     *
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   706
     * @param years  the number of years, may be negative
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   707
     * @param months  the number of years, may be negative
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   708
     * @param days  the number of years, may be negative
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   709
     * @return the period in terms of this chronology, not null
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   710
     */
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   711
    default ChronoPeriod period(int years, int months, int days) {
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   712
        return new ChronoPeriodImpl(this, years, months, days);
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   713
    }
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   714
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   715
    //-----------------------------------------------------------------------
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19841
diff changeset
   716
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   717
     * Compares this chronology to another chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   718
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   719
     * The comparison order first by the chronology ID string, then by any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   720
     * additional information specific to the subclass.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   721
     * It is "consistent with equals", as defined by {@link Comparable}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   722
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   723
     * @param other  the other chronology to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   724
     * @return the comparator value, negative if less, positive if greater
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   725
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   726
    @Override
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   727
    int compareTo(Chronology other);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   728
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   729
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   730
     * Checks if this chronology is equal to another chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   731
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   732
     * The comparison is based on the entire state of the object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   733
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   734
     * @param obj  the object to check, null returns false
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   735
     * @return true if this is equal to the other chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   736
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   737
    @Override
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   738
    boolean equals(Object obj);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   739
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   740
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   741
     * A hash code for this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   742
     * <p>
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   743
     * The hash code should be based on the entire state of the object.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   744
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   745
     * @return a suitable hash code
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   746
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   747
    @Override
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   748
    int hashCode();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   749
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   750
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   751
    /**
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   752
     * Outputs this chronology as a {@code String}.
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   753
     * <p>
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   754
     * The format should include the entire state of the object.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   755
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   756
     * @return a string representation of this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   757
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   758
    @Override
20794
ec823009c5f7 8025719: Change Chronology to an interface
scolebourne
parents: 20519
diff changeset
   759
    String toString();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   760
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   761
}