2013-01-22 20:59:21 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is available under and governed by the GNU General Public
|
|
|
|
* License version 2 only, as published by the Free Software Foundation.
|
|
|
|
* However, the following notice accompanied the original version of this
|
|
|
|
* file:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of JSR-310 nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
package java.time.chrono;
|
2013-01-22 20:59:21 -08:00
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
|
|
|
|
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
|
|
|
|
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
|
|
|
|
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
|
|
|
|
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
|
|
|
|
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
|
|
|
|
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
|
|
|
|
import static java.time.temporal.ChronoField.EPOCH_DAY;
|
|
|
|
import static java.time.temporal.ChronoField.ERA;
|
|
|
|
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
|
|
|
|
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
|
|
|
|
import static java.time.temporal.ChronoField.YEAR;
|
|
|
|
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
|
2013-07-18 11:02:02 -07:00
|
|
|
import static java.time.temporal.ChronoUnit.DAYS;
|
|
|
|
import static java.time.temporal.ChronoUnit.MONTHS;
|
|
|
|
import static java.time.temporal.ChronoUnit.WEEKS;
|
2013-04-12 07:57:35 -07:00
|
|
|
import static java.time.temporal.TemporalAdjuster.nextOrSame;
|
|
|
|
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.io.DataInput;
|
|
|
|
import java.io.DataOutput;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InvalidObjectException;
|
|
|
|
import java.io.ObjectStreamException;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.io.Serializable;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.time.Clock;
|
|
|
|
import java.time.DateTimeException;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.time.DayOfWeek;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.time.LocalTime;
|
2013-07-18 11:02:02 -07:00
|
|
|
import java.time.Month;
|
|
|
|
import java.time.Year;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.time.ZoneId;
|
|
|
|
import java.time.format.DateTimeFormatterBuilder;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.time.format.ResolverStyle;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.time.format.TextStyle;
|
2013-02-12 09:25:43 -08:00
|
|
|
import java.time.temporal.ChronoField;
|
|
|
|
import java.time.temporal.Temporal;
|
|
|
|
import java.time.temporal.TemporalAccessor;
|
2013-07-18 11:02:02 -07:00
|
|
|
import java.time.temporal.TemporalAdjuster;
|
2013-02-12 09:25:43 -08:00
|
|
|
import java.time.temporal.TemporalField;
|
|
|
|
import java.time.temporal.TemporalQuery;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.time.temporal.UnsupportedTemporalTypeException;
|
2013-02-12 09:25:43 -08:00
|
|
|
import java.time.temporal.ValueRange;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.util.Comparator;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2013-04-12 07:57:35 -07:00
|
|
|
import java.util.Map;
|
2013-01-22 20:59:21 -08:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.ServiceLoader;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
import sun.util.logging.PlatformLogger;
|
|
|
|
|
2013-01-22 20:59:21 -08:00
|
|
|
/**
|
|
|
|
* A calendar system, used to organize and identify dates.
|
|
|
|
* <p>
|
|
|
|
* The main date and time API is built on the ISO calendar system.
|
|
|
|
* This class operates behind the scenes to represent the general concept of a calendar system.
|
|
|
|
* For example, the Japanese, Minguo, Thai Buddhist and others.
|
|
|
|
* <p>
|
|
|
|
* Most other calendar systems also operate on the shared concepts of year, month and day,
|
|
|
|
* linked to the cycles of the Earth around the Sun, and the Moon around the Earth.
|
2013-02-12 09:25:43 -08:00
|
|
|
* These shared concepts are defined by {@link ChronoField} and are available
|
|
|
|
* for use by any {@code Chronology} implementation:
|
2013-01-22 20:59:21 -08:00
|
|
|
* <pre>
|
|
|
|
* LocalDate isoDate = ...
|
2013-04-12 07:57:35 -07:00
|
|
|
* ThaiBuddhistDate thaiDate = ...
|
2013-01-22 20:59:21 -08:00
|
|
|
* int isoYear = isoDate.get(ChronoField.YEAR);
|
|
|
|
* int thaiYear = thaiDate.get(ChronoField.YEAR);
|
|
|
|
* </pre>
|
|
|
|
* As shown, although the date objects are in different calendar systems, represented by different
|
2013-02-12 09:25:43 -08:00
|
|
|
* {@code Chronology} instances, both can be queried using the same constant on {@code ChronoField}.
|
2013-01-22 20:59:21 -08:00
|
|
|
* For a full discussion of the implications of this, see {@link ChronoLocalDate}.
|
|
|
|
* In general, the advice is to use the known ISO-based {@code LocalDate}, rather than
|
|
|
|
* {@code ChronoLocalDate}.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* While a {@code Chronology} object typically uses {@code ChronoField} and is based on
|
2013-01-22 20:59:21 -08:00
|
|
|
* an era, year-of-era, month-of-year, day-of-month model of a date, this is not required.
|
2013-02-12 09:25:43 -08:00
|
|
|
* A {@code Chronology} instance may represent a totally different kind of calendar system,
|
2013-01-22 20:59:21 -08:00
|
|
|
* such as the Mayan.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* In practical terms, the {@code Chronology} instance also acts as a factory.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The {@link #of(String)} method allows an instance to be looked up by identifier,
|
|
|
|
* while the {@link #ofLocale(Locale)} method allows lookup by locale.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* The {@code Chronology} instance provides a set of methods to create {@code ChronoLocalDate} instances.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The date classes are used to manipulate specific dates.
|
|
|
|
* <p><ul>
|
|
|
|
* <li> {@link #dateNow() dateNow()}
|
|
|
|
* <li> {@link #dateNow(Clock) dateNow(clock)}
|
|
|
|
* <li> {@link #dateNow(ZoneId) dateNow(zone)}
|
|
|
|
* <li> {@link #date(int, int, int) date(yearProleptic, month, day)}
|
|
|
|
* <li> {@link #date(Era, int, int, int) date(era, yearOfEra, month, day)}
|
|
|
|
* <li> {@link #dateYearDay(int, int) dateYearDay(yearProleptic, dayOfYear)}
|
|
|
|
* <li> {@link #dateYearDay(Era, int, int) dateYearDay(era, yearOfEra, dayOfYear)}
|
|
|
|
* <li> {@link #date(TemporalAccessor) date(TemporalAccessor)}
|
|
|
|
* </ul><p>
|
|
|
|
*
|
2013-02-12 09:25:43 -08:00
|
|
|
* <h3 id="addcalendars">Adding New Calendars</h3>
|
2013-01-22 20:59:21 -08:00
|
|
|
* The set of available chronologies can be extended by applications.
|
|
|
|
* Adding a new calendar system requires the writing of an implementation of
|
2013-02-12 09:25:43 -08:00
|
|
|
* {@code Chronology}, {@code ChronoLocalDate} and {@code Era}.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The majority of the logic specific to the calendar system will be in
|
2013-02-12 09:25:43 -08:00
|
|
|
* {@code ChronoLocalDate}. The {@code Chronology} subclass acts as a factory.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
|
|
|
* To permit the discovery of additional chronologies, the {@link java.util.ServiceLoader ServiceLoader}
|
|
|
|
* is used. A file must be added to the {@code META-INF/services} directory with the
|
2013-02-12 09:25:43 -08:00
|
|
|
* name 'java.time.chrono.Chronology' listing the implementation classes.
|
2013-01-22 20:59:21 -08:00
|
|
|
* See the ServiceLoader for more details on service loading.
|
|
|
|
* For lookup by id or calendarType, the system provided calendars are found
|
|
|
|
* first followed by application provided calendars.
|
|
|
|
* <p>
|
|
|
|
* Each chronology must define a chronology ID that is unique within the system.
|
|
|
|
* If the chronology represents a calendar system defined by the
|
2013-04-12 07:57:35 -07:00
|
|
|
* CLDR specification then the calendar type is the concatenation of the
|
|
|
|
* CLDR type and, if applicable, the CLDR variant,
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
2013-05-15 07:48:57 -07:00
|
|
|
* @implSpec
|
2013-01-22 20:59:21 -08:00
|
|
|
* This class must be implemented with care to ensure other classes operate correctly.
|
|
|
|
* All implementations that can be instantiated must be final, immutable and thread-safe.
|
|
|
|
* Subclasses should be Serializable wherever possible.
|
|
|
|
*
|
|
|
|
* @since 1.8
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public abstract class Chronology implements Comparable<Chronology> {
|
2013-01-22 20:59:21 -08:00
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
/**
|
|
|
|
* ChronoLocalDate order constant.
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
static final Comparator<ChronoLocalDate> DATE_ORDER =
|
|
|
|
(Comparator<ChronoLocalDate> & Serializable) (date1, date2) -> {
|
2013-04-12 07:57:35 -07:00
|
|
|
return Long.compare(date1.toEpochDay(), date2.toEpochDay());
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* ChronoLocalDateTime order constant.
|
|
|
|
*/
|
|
|
|
static final Comparator<ChronoLocalDateTime<?>> DATE_TIME_ORDER =
|
|
|
|
(Comparator<ChronoLocalDateTime<?>> & Serializable) (dateTime1, dateTime2) -> {
|
|
|
|
int cmp = Long.compare(dateTime1.toLocalDate().toEpochDay(), dateTime2.toLocalDate().toEpochDay());
|
|
|
|
if (cmp == 0) {
|
|
|
|
cmp = Long.compare(dateTime1.toLocalTime().toNanoOfDay(), dateTime2.toLocalTime().toNanoOfDay());
|
|
|
|
}
|
|
|
|
return cmp;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* ChronoZonedDateTime order constant.
|
|
|
|
*/
|
|
|
|
static final Comparator<ChronoZonedDateTime<?>> INSTANT_ORDER =
|
|
|
|
(Comparator<ChronoZonedDateTime<?>> & Serializable) (dateTime1, dateTime2) -> {
|
|
|
|
int cmp = Long.compare(dateTime1.toEpochSecond(), dateTime2.toEpochSecond());
|
|
|
|
if (cmp == 0) {
|
|
|
|
cmp = Long.compare(dateTime1.toLocalTime().getNano(), dateTime2.toLocalTime().getNano());
|
|
|
|
}
|
|
|
|
return cmp;
|
|
|
|
};
|
|
|
|
|
2013-01-22 20:59:21 -08:00
|
|
|
/**
|
|
|
|
* Map of available calendars by ID.
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<>();
|
2013-01-22 20:59:21 -08:00
|
|
|
/**
|
|
|
|
* Map of available calendars by calendar type.
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<>();
|
2013-01-22 20:59:21 -08:00
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
/**
|
|
|
|
* Register a Chronology by its ID and type for lookup by {@link #of(java.lang.String)}.
|
|
|
|
* Chronologies must not be registered until they are completely constructed.
|
|
|
|
* Specifically, not in the constructor of Chronology.
|
|
|
|
*
|
|
|
|
* @param chrono the chronology to register; not null
|
|
|
|
* @return the already registered Chronology if any, may be null
|
|
|
|
*/
|
|
|
|
static Chronology registerChrono(Chronology chrono) {
|
|
|
|
return registerChrono(chrono, chrono.getId());
|
|
|
|
}
|
|
|
|
|
2013-01-22 20:59:21 -08:00
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Register a Chronology by ID and type for lookup by {@link #of(java.lang.String)}.
|
2013-01-22 20:59:21 -08:00
|
|
|
* Chronos must not be registered until they are completely constructed.
|
2013-02-12 09:25:43 -08:00
|
|
|
* Specifically, not in the constructor of Chronology.
|
2013-04-12 07:57:35 -07:00
|
|
|
*
|
2013-01-22 20:59:21 -08:00
|
|
|
* @param chrono the chronology to register; not null
|
2013-04-12 07:57:35 -07:00
|
|
|
* @param id the ID to register the chronology; not null
|
|
|
|
* @return the already registered Chronology if any, may be null
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-04-12 07:57:35 -07:00
|
|
|
static Chronology registerChrono(Chronology chrono, String id) {
|
|
|
|
Chronology prev = CHRONOS_BY_ID.putIfAbsent(id, chrono);
|
2013-01-22 20:59:21 -08:00
|
|
|
if (prev == null) {
|
|
|
|
String type = chrono.getCalendarType();
|
|
|
|
if (type != null) {
|
|
|
|
CHRONOS_BY_TYPE.putIfAbsent(type, chrono);
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
return prev;
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Initialization of the maps from id and type to Chronology.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The ServiceLoader is used to find and register any implementations
|
2013-02-12 09:25:43 -08:00
|
|
|
* of {@link java.time.chrono.Chronology} found in the bootclass loader.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The built-in chronologies are registered explicitly.
|
|
|
|
* Calendars configured via the Thread's context classloader are local
|
|
|
|
* to that thread and are ignored.
|
|
|
|
* <p>
|
|
|
|
* The initialization is done only once using the registration
|
2013-02-12 09:25:43 -08:00
|
|
|
* of the IsoChronology as the test and the final step.
|
2013-01-22 20:59:21 -08:00
|
|
|
* Multiple threads may perform the initialization concurrently.
|
2013-02-12 09:25:43 -08:00
|
|
|
* Only the first registration of each Chronology is retained by the
|
2013-01-22 20:59:21 -08:00
|
|
|
* ConcurrentHashMap.
|
|
|
|
* @return true if the cache was initialized
|
|
|
|
*/
|
|
|
|
private static boolean initCache() {
|
|
|
|
if (CHRONOS_BY_ID.get("ISO") == null) {
|
|
|
|
// Initialization is incomplete
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
// Register built-in Chronologies
|
2013-02-12 09:25:43 -08:00
|
|
|
registerChrono(HijrahChronology.INSTANCE);
|
|
|
|
registerChrono(JapaneseChronology.INSTANCE);
|
|
|
|
registerChrono(MinguoChronology.INSTANCE);
|
|
|
|
registerChrono(ThaiBuddhistChronology.INSTANCE);
|
2013-01-22 20:59:21 -08:00
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
// Register Chronologies from the ServiceLoader
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class, null);
|
|
|
|
for (Chronology chrono : loader) {
|
|
|
|
String id = chrono.getId();
|
|
|
|
if (id.equals("ISO") || registerChrono(chrono) != null) {
|
|
|
|
// Log the attempt to replace an existing Chronology
|
|
|
|
PlatformLogger logger = PlatformLogger.getLogger("java.time.chrono");
|
|
|
|
logger.warning("Ignoring duplicate Chronology, from ServiceLoader configuration " + id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 09:25:43 -08:00
|
|
|
// finally, register IsoChronology to mark initialization is complete
|
|
|
|
registerChrono(IsoChronology.INSTANCE);
|
2013-01-22 20:59:21 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Obtains an instance of {@code Chronology} from a temporal object.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* This obtains a chronology based on the specified temporal.
|
|
|
|
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
|
|
|
|
* which this factory converts to an instance of {@code Chronology}.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* The conversion will obtain the chronology using {@link TemporalQuery#chronology()}.
|
2013-02-12 09:25:43 -08:00
|
|
|
* If the specified temporal object does not have a chronology, {@link IsoChronology} is returned.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
|
|
|
* This method matches the signature of the functional interface {@link TemporalQuery}
|
2013-02-12 09:25:43 -08:00
|
|
|
* allowing it to be used in queries via method reference, {@code Chronology::from}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param temporal the temporal to convert, not null
|
|
|
|
* @return the chronology, not null
|
2013-02-12 09:25:43 -08:00
|
|
|
* @throws DateTimeException if unable to convert to an {@code Chronology}
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public static Chronology from(TemporalAccessor temporal) {
|
2013-01-22 20:59:21 -08:00
|
|
|
Objects.requireNonNull(temporal, "temporal");
|
2013-04-12 07:57:35 -07:00
|
|
|
Chronology obj = temporal.query(TemporalQuery.chronology());
|
2013-02-12 09:25:43 -08:00
|
|
|
return (obj != null ? obj : IsoChronology.INSTANCE);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Obtains an instance of {@code Chronology} from a locale.
|
|
|
|
* <p>
|
|
|
|
* This returns a {@code Chronology} based on the specified locale,
|
|
|
|
* typically returning {@code IsoChronology}. Other calendar systems
|
|
|
|
* are only returned if they are explicitly selected within the locale.
|
|
|
|
* <p>
|
|
|
|
* The {@link Locale} class provide access to a range of information useful
|
|
|
|
* for localizing an application. This includes the language and region,
|
|
|
|
* such as "en-GB" for English as used in Great Britain.
|
|
|
|
* <p>
|
|
|
|
* The {@code Locale} class also supports an extension mechanism that
|
|
|
|
* can be used to identify a calendar system. The mechanism is a form
|
2013-05-15 07:48:57 -07:00
|
|
|
* of key-value pairs, where the calendar system has the key "ca".
|
2013-02-12 09:25:43 -08:00
|
|
|
* For example, the locale "en-JP-u-ca-japanese" represents the English
|
|
|
|
* language as used in Japan with the Japanese calendar system.
|
|
|
|
* <p>
|
|
|
|
* This method finds the desired calendar system by in a manner equivalent
|
|
|
|
* to passing "ca" to {@link Locale#getUnicodeLocaleType(String)}.
|
|
|
|
* If the "ca" key is not present, then {@code IsoChronology} is returned.
|
|
|
|
* <p>
|
|
|
|
* Note that the behavior of this method differs from the older
|
|
|
|
* {@link java.util.Calendar#getInstance(Locale)} method.
|
|
|
|
* If that method receives a locale of "th_TH" it will return {@code BuddhistCalendar}.
|
|
|
|
* By contrast, this method will return {@code IsoChronology}.
|
|
|
|
* Passing the locale "th-TH-u-ca-buddhist" into either method will
|
|
|
|
* result in the Thai Buddhist calendar system and is therefore the
|
|
|
|
* recommended approach going forward for Thai calendar system localization.
|
|
|
|
* <p>
|
|
|
|
* A similar, but simpler, situation occurs for the Japanese calendar system.
|
|
|
|
* The locale "jp_JP_JP" has previously been used to access the calendar.
|
|
|
|
* However, unlike the Thai locale, "ja_JP_JP" is automatically converted by
|
|
|
|
* {@code Locale} to the modern and recommended form of "ja-JP-u-ca-japanese".
|
|
|
|
* Thus, there is no difference in behavior between this method and
|
|
|
|
* {@code Calendar#getInstance(Locale)}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param locale the locale to use to obtain the calendar system, not null
|
|
|
|
* @return the calendar system associated with the locale, not null
|
|
|
|
* @throws DateTimeException if the locale-specified calendar cannot be found
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public static Chronology ofLocale(Locale locale) {
|
2013-01-22 20:59:21 -08:00
|
|
|
Objects.requireNonNull(locale, "locale");
|
|
|
|
String type = locale.getUnicodeLocaleType("ca");
|
2013-02-12 09:25:43 -08:00
|
|
|
if (type == null || "iso".equals(type) || "iso8601".equals(type)) {
|
|
|
|
return IsoChronology.INSTANCE;
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
2013-02-12 09:25:43 -08:00
|
|
|
// Not pre-defined; lookup by the type
|
|
|
|
do {
|
|
|
|
Chronology chrono = CHRONOS_BY_TYPE.get(type);
|
|
|
|
if (chrono != null) {
|
|
|
|
return chrono;
|
|
|
|
}
|
|
|
|
// If not found, do the initialization (once) and repeat the lookup
|
|
|
|
} while (initCache());
|
2013-04-12 07:57:35 -07:00
|
|
|
|
|
|
|
// Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader
|
|
|
|
// Application provided Chronologies must not be cached
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
|
|
|
|
for (Chronology chrono : loader) {
|
|
|
|
if (type.equals(chrono.getCalendarType())) {
|
|
|
|
return chrono;
|
|
|
|
}
|
|
|
|
}
|
2013-02-12 09:25:43 -08:00
|
|
|
throw new DateTimeException("Unknown calendar system: " + type);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Obtains an instance of {@code Chronology} from a chronology ID or
|
2013-01-22 20:59:21 -08:00
|
|
|
* calendar system type.
|
|
|
|
* <p>
|
|
|
|
* This returns a chronology based on either the ID or the type.
|
|
|
|
* The {@link #getId() chronology ID} uniquely identifies the chronology.
|
2013-04-12 07:57:35 -07:00
|
|
|
* The {@link #getCalendarType() calendar system type} is defined by the
|
|
|
|
* CLDR specification.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
|
|
|
* The chronology may be a system chronology or a chronology
|
|
|
|
* provided by the application via ServiceLoader configuration.
|
|
|
|
* <p>
|
|
|
|
* Since some calendars can be customized, the ID or type typically refers
|
|
|
|
* to the default customization. For example, the Gregorian calendar can have multiple
|
|
|
|
* cutover dates from the Julian, but the lookup only provides the default cutover date.
|
|
|
|
*
|
|
|
|
* @param id the chronology ID or calendar system type, not null
|
|
|
|
* @return the chronology with the identifier requested, not null
|
|
|
|
* @throws DateTimeException if the chronology cannot be found
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public static Chronology of(String id) {
|
2013-01-22 20:59:21 -08:00
|
|
|
Objects.requireNonNull(id, "id");
|
|
|
|
do {
|
2013-02-12 09:25:43 -08:00
|
|
|
Chronology chrono = of0(id);
|
2013-01-22 20:59:21 -08:00
|
|
|
if (chrono != null) {
|
|
|
|
return chrono;
|
|
|
|
}
|
|
|
|
// If not found, do the initialization (once) and repeat the lookup
|
|
|
|
} while (initCache());
|
|
|
|
|
2013-02-12 09:25:43 -08:00
|
|
|
// Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader
|
2013-01-22 20:59:21 -08:00
|
|
|
// Application provided Chronologies must not be cached
|
|
|
|
@SuppressWarnings("rawtypes")
|
2013-02-12 09:25:43 -08:00
|
|
|
ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
|
|
|
|
for (Chronology chrono : loader) {
|
2013-01-22 20:59:21 -08:00
|
|
|
if (id.equals(chrono.getId()) || id.equals(chrono.getCalendarType())) {
|
|
|
|
return chrono;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new DateTimeException("Unknown chronology: " + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Obtains an instance of {@code Chronology} from a chronology ID or
|
2013-01-22 20:59:21 -08:00
|
|
|
* calendar system type.
|
|
|
|
*
|
|
|
|
* @param id the chronology ID or calendar system type, not null
|
|
|
|
* @return the chronology with the identifier requested, or {@code null} if not found
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
private static Chronology of0(String id) {
|
|
|
|
Chronology chrono = CHRONOS_BY_ID.get(id);
|
2013-01-22 20:59:21 -08:00
|
|
|
if (chrono == null) {
|
|
|
|
chrono = CHRONOS_BY_TYPE.get(id);
|
|
|
|
}
|
|
|
|
return chrono;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the available chronologies.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* Each returned {@code Chronology} is available for use in the system.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The set of chronologies includes the system chronologies and
|
|
|
|
* any chronologies provided by the application via ServiceLoader
|
|
|
|
* configuration.
|
|
|
|
*
|
|
|
|
* @return the independent, modifiable set of the available chronology IDs, not null
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public static Set<Chronology> getAvailableChronologies() {
|
2013-01-22 20:59:21 -08:00
|
|
|
initCache(); // force initialization
|
2013-04-12 07:57:35 -07:00
|
|
|
HashSet<Chronology> chronos = new HashSet<>(CHRONOS_BY_ID.values());
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
/// Add in Chronologies from the ServiceLoader configuration
|
|
|
|
@SuppressWarnings("rawtypes")
|
2013-02-12 09:25:43 -08:00
|
|
|
ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
|
|
|
|
for (Chronology chrono : loader) {
|
2013-01-22 20:59:21 -08:00
|
|
|
chronos.add(chrono);
|
|
|
|
}
|
|
|
|
return chronos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Creates an instance.
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
protected Chronology() {
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Gets the ID of the chronology.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* The ID uniquely identifies the {@code Chronology}.
|
|
|
|
* It can be used to lookup the {@code Chronology} using {@link #of(String)}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @return the chronology ID, not null
|
|
|
|
* @see #getCalendarType()
|
|
|
|
*/
|
|
|
|
public abstract String getId();
|
|
|
|
|
|
|
|
/**
|
2013-04-12 07:57:35 -07:00
|
|
|
* Gets the calendar type of the calendar system.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* The calendar type is an identifier defined by the CLDR and
|
|
|
|
* <em>Unicode Locale Data Markup Language (LDML)</em> specifications
|
|
|
|
* to uniquely identification a calendar.
|
|
|
|
* The {@code getCalendarType} is the concatenation of the CLDR calendar type
|
|
|
|
* and the variant, if applicable, is appended separated by "-".
|
|
|
|
* The calendar type is used to lookup the {@code Chronology} using {@link #of(String)}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
2013-05-15 07:48:57 -07:00
|
|
|
* @return the calendar system type, null if the calendar is not defined by CLDR/LDML
|
2013-01-22 20:59:21 -08:00
|
|
|
* @see #getId()
|
|
|
|
*/
|
|
|
|
public abstract String getCalendarType();
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Obtains a local date in this chronology from the era, year-of-era,
|
|
|
|
* month-of-year and day-of-month fields.
|
|
|
|
*
|
|
|
|
* @param era the era of the correct type for the chronology, not null
|
|
|
|
* @param yearOfEra the chronology year-of-era
|
|
|
|
* @param month the chronology month-of-year
|
|
|
|
* @param dayOfMonth the chronology day-of-month
|
|
|
|
* @return the local date in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
2013-04-12 07:57:35 -07:00
|
|
|
* @throws ClassCastException if the {@code era} is not of the correct type for the chronology
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains a local date in this chronology from the proleptic-year,
|
|
|
|
* month-of-year and day-of-month fields.
|
|
|
|
*
|
|
|
|
* @param prolepticYear the chronology proleptic-year
|
|
|
|
* @param month the chronology month-of-year
|
|
|
|
* @param dayOfMonth the chronology day-of-month
|
|
|
|
* @return the local date in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public abstract ChronoLocalDate date(int prolepticYear, int month, int dayOfMonth);
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains a local date in this chronology from the era, year-of-era and
|
|
|
|
* day-of-year fields.
|
|
|
|
*
|
|
|
|
* @param era the era of the correct type for the chronology, not null
|
|
|
|
* @param yearOfEra the chronology year-of-era
|
|
|
|
* @param dayOfYear the chronology day-of-year
|
|
|
|
* @return the local date in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
2013-04-12 07:57:35 -07:00
|
|
|
* @throws ClassCastException if the {@code era} is not of the correct type for the chronology
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains a local date in this chronology from the proleptic-year and
|
|
|
|
* day-of-year fields.
|
|
|
|
*
|
|
|
|
* @param prolepticYear the chronology proleptic-year
|
|
|
|
* @param dayOfYear the chronology day-of-year
|
|
|
|
* @return the local date in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public abstract ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear);
|
2013-04-12 07:57:35 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains a local date in this chronology from the epoch-day.
|
|
|
|
* <p>
|
|
|
|
* The definition of {@link ChronoField#EPOCH_DAY EPOCH_DAY} is the same
|
|
|
|
* for all calendar systems, thus it can be used for conversion.
|
|
|
|
*
|
|
|
|
* @param epochDay the epoch day
|
|
|
|
* @return the local date in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public abstract ChronoLocalDate dateEpochDay(long epochDay);
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Obtains the current local date in this chronology from the system clock in the default time-zone.
|
|
|
|
* <p>
|
|
|
|
* This will query the {@link Clock#systemDefaultZone() system clock} in the default
|
|
|
|
* time-zone to obtain the current date.
|
|
|
|
* <p>
|
|
|
|
* Using this method will prevent the ability to use an alternate clock for testing
|
|
|
|
* because the clock is hard-coded.
|
|
|
|
* <p>
|
|
|
|
* This implementation uses {@link #dateNow(Clock)}.
|
|
|
|
*
|
|
|
|
* @return the current local date using the system clock and default time-zone, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDate dateNow() {
|
2013-01-22 20:59:21 -08:00
|
|
|
return dateNow(Clock.systemDefaultZone());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the current local date in this chronology from the system clock in the specified time-zone.
|
|
|
|
* <p>
|
|
|
|
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
|
|
|
|
* Specifying the time-zone avoids dependence on the default time-zone.
|
|
|
|
* <p>
|
|
|
|
* Using this method will prevent the ability to use an alternate clock for testing
|
|
|
|
* because the clock is hard-coded.
|
|
|
|
*
|
|
|
|
* @param zone the zone ID to use, not null
|
|
|
|
* @return the current local date using the system clock, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDate dateNow(ZoneId zone) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return dateNow(Clock.system(zone));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the current local date in this chronology from the specified clock.
|
|
|
|
* <p>
|
|
|
|
* This will query the specified clock to obtain the current date - today.
|
|
|
|
* Using this method allows the use of an alternate clock for testing.
|
|
|
|
* The alternate clock may be introduced using {@link Clock dependency injection}.
|
|
|
|
*
|
|
|
|
* @param clock the clock to use, not null
|
|
|
|
* @return the current local date, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDate dateNow(Clock clock) {
|
2013-01-22 20:59:21 -08:00
|
|
|
Objects.requireNonNull(clock, "clock");
|
|
|
|
return date(LocalDate.now(clock));
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2013-02-12 09:25:43 -08:00
|
|
|
/**
|
|
|
|
* Obtains a local date in this chronology from another temporal object.
|
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* This obtains a date in this chronology based on the specified temporal.
|
2013-02-12 09:25:43 -08:00
|
|
|
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
|
|
|
|
* which this factory converts to an instance of {@code ChronoLocalDate}.
|
|
|
|
* <p>
|
|
|
|
* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
|
|
|
|
* field, which is standardized across calendar systems.
|
|
|
|
* <p>
|
|
|
|
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
|
|
* allowing it to be used as a query via method reference, {@code aChronology::date}.
|
|
|
|
*
|
|
|
|
* @param temporal the temporal object to convert, not null
|
|
|
|
* @return the local date in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date
|
2013-04-12 07:57:35 -07:00
|
|
|
* @see ChronoLocalDate#from(TemporalAccessor)
|
2013-02-12 09:25:43 -08:00
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public abstract ChronoLocalDate date(TemporalAccessor temporal);
|
2013-02-12 09:25:43 -08:00
|
|
|
|
2013-01-22 20:59:21 -08:00
|
|
|
/**
|
|
|
|
* Obtains a local date-time in this chronology from another temporal object.
|
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* This obtains a date-time in this chronology based on the specified temporal.
|
2013-02-12 09:25:43 -08:00
|
|
|
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
|
|
|
|
* which this factory converts to an instance of {@code ChronoLocalDateTime}.
|
|
|
|
* <p>
|
|
|
|
* The conversion extracts and combines the {@code ChronoLocalDate} and the
|
|
|
|
* {@code LocalTime} from the temporal object.
|
|
|
|
* Implementations are permitted to perform optimizations such as accessing
|
|
|
|
* those fields that are equivalent to the relevant objects.
|
|
|
|
* The result uses this chronology.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
|
|
* allowing it to be used as a query via method reference, {@code aChronology::localDateTime}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param temporal the temporal object to convert, not null
|
|
|
|
* @return the local date-time in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date-time
|
2013-04-12 07:57:35 -07:00
|
|
|
* @see ChronoLocalDateTime#from(TemporalAccessor)
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDateTime<? extends ChronoLocalDate> localDateTime(TemporalAccessor temporal) {
|
2013-01-22 20:59:21 -08:00
|
|
|
try {
|
|
|
|
return date(temporal).atTime(LocalTime.from(temporal));
|
|
|
|
} catch (DateTimeException ex) {
|
|
|
|
throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass(), ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* This obtains a zoned date-time in this chronology based on the specified temporal.
|
2013-02-12 09:25:43 -08:00
|
|
|
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
|
|
|
|
* which this factory converts to an instance of {@code ChronoZonedDateTime}.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* The conversion will first obtain a {@code ZoneId} from the temporal object,
|
|
|
|
* falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
|
|
|
|
* an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
|
|
|
|
* The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
|
|
|
|
* with {@code Instant} or {@code ChronoLocalDateTime}.
|
|
|
|
* Implementations are permitted to perform optimizations such as accessing
|
|
|
|
* those fields that are equivalent to the relevant objects.
|
|
|
|
* The result uses this chronology.
|
|
|
|
* <p>
|
|
|
|
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
|
|
* allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param temporal the temporal object to convert, not null
|
|
|
|
* @return the zoned date-time in this chronology, not null
|
|
|
|
* @throws DateTimeException if unable to create the date-time
|
2013-04-12 07:57:35 -07:00
|
|
|
* @see ChronoZonedDateTime#from(TemporalAccessor)
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
|
2013-01-22 20:59:21 -08:00
|
|
|
try {
|
|
|
|
ZoneId zone = ZoneId.from(temporal);
|
|
|
|
try {
|
|
|
|
Instant instant = Instant.from(temporal);
|
|
|
|
return zonedDateTime(instant, zone);
|
|
|
|
|
|
|
|
} catch (DateTimeException ex1) {
|
2013-07-18 11:02:02 -07:00
|
|
|
ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
|
2013-01-22 20:59:21 -08:00
|
|
|
return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
|
|
|
|
}
|
|
|
|
} catch (DateTimeException ex) {
|
|
|
|
throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-12 09:25:43 -08:00
|
|
|
* Obtains a {@code ChronoZonedDateTime} in this chronology from an {@code Instant}.
|
2013-01-22 20:59:21 -08:00
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* This obtains a zoned date-time with the same instant as that specified.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param instant the instant to create the date-time from, not null
|
|
|
|
* @param zone the time-zone, not null
|
|
|
|
* @return the zoned date-time, not null
|
|
|
|
* @throws DateTimeException if the result exceeds the supported range
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(Instant instant, ZoneId zone) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return ChronoZonedDateTimeImpl.ofInstant(this, instant, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Checks if the specified year is a leap year.
|
|
|
|
* <p>
|
|
|
|
* A leap-year is a year of a longer length than normal.
|
|
|
|
* The exact meaning is determined by the chronology according to the following constraints.
|
|
|
|
* <p><ul>
|
|
|
|
* <li>a leap-year must imply a year-length longer than a non leap-year.
|
|
|
|
* <li>a chronology that does not support the concept of a year must return false.
|
|
|
|
* </ul><p>
|
|
|
|
*
|
|
|
|
* @param prolepticYear the proleptic-year to check, not validated for range
|
|
|
|
* @return true if the year is a leap year
|
|
|
|
*/
|
|
|
|
public abstract boolean isLeapYear(long prolepticYear);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the proleptic-year given the era and year-of-era.
|
|
|
|
* <p>
|
|
|
|
* This combines the era and year-of-era into the single proleptic-year field.
|
2013-04-12 07:57:35 -07:00
|
|
|
* <p>
|
|
|
|
* If the chronology makes active use of eras, such as {@code JapaneseChronology}
|
|
|
|
* then the year-of-era will be validated against the era.
|
|
|
|
* For other chronologies, validation is optional.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param era the era of the correct type for the chronology, not null
|
|
|
|
* @param yearOfEra the chronology year-of-era
|
|
|
|
* @return the proleptic-year
|
2013-04-12 07:57:35 -07:00
|
|
|
* @throws DateTimeException if unable to convert to a proleptic-year,
|
|
|
|
* such as if the year is invalid for the era
|
|
|
|
* @throws ClassCastException if the {@code era} is not of the correct type for the chronology
|
2013-01-22 20:59:21 -08:00
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public abstract int prolepticYear(Era era, int yearOfEra);
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the chronology era object from the numeric value.
|
|
|
|
* <p>
|
|
|
|
* The era is, conceptually, the largest division of the time-line.
|
|
|
|
* Most calendar systems have a single epoch dividing the time-line into two eras.
|
|
|
|
* However, some have multiple eras, such as one for the reign of each leader.
|
|
|
|
* The exact meaning is determined by the chronology according to the following constraints.
|
|
|
|
* <p>
|
|
|
|
* The era in use at 1970-01-01 must have the value 1.
|
|
|
|
* Later eras must have sequentially higher values.
|
|
|
|
* Earlier eras must have sequentially lower values.
|
|
|
|
* Each chronology must refer to an enum or similar singleton to provide the era values.
|
|
|
|
* <p>
|
|
|
|
* This method returns the singleton era of the correct type for the specified era value.
|
|
|
|
*
|
|
|
|
* @param eraValue the era value
|
|
|
|
* @return the calendar system era, not null
|
|
|
|
* @throws DateTimeException if unable to create the era
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public abstract Era eraOf(int eraValue);
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of eras for the chronology.
|
|
|
|
* <p>
|
|
|
|
* Most calendar systems have an era, within which the year has meaning.
|
|
|
|
* If the calendar system does not support the concept of eras, an empty
|
|
|
|
* list must be returned.
|
|
|
|
*
|
|
|
|
* @return the list of eras for the chronology, may be immutable, not null
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public abstract List<Era> eras();
|
2013-01-22 20:59:21 -08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Gets the range of valid values for the specified field.
|
|
|
|
* <p>
|
|
|
|
* All fields can be expressed as a {@code long} integer.
|
|
|
|
* This method returns an object that describes the valid range for that value.
|
|
|
|
* <p>
|
|
|
|
* Note that the result only describes the minimum and maximum valid values
|
|
|
|
* and it is important not to read too much into them. For example, there
|
|
|
|
* could be values within the range that are invalid for the field.
|
|
|
|
* <p>
|
|
|
|
* This method will return a result whether or not the chronology supports the field.
|
|
|
|
*
|
|
|
|
* @param field the field to get the range for, not null
|
|
|
|
* @return the range of valid values for the field, not null
|
|
|
|
* @throws DateTimeException if the range for the field cannot be obtained
|
|
|
|
*/
|
|
|
|
public abstract ValueRange range(ChronoField field);
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Gets the textual representation of this chronology.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* This returns the textual name used to identify the chronology,
|
|
|
|
* suitable for presentation to the user.
|
2013-01-22 20:59:21 -08:00
|
|
|
* The parameters control the style of the returned text and the locale.
|
|
|
|
*
|
|
|
|
* @param style the style of the text required, not null
|
|
|
|
* @param locale the locale to use, not null
|
|
|
|
* @return the text value of the chronology, not null
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
public String getDisplayName(TextStyle style, Locale locale) {
|
2013-04-12 07:57:35 -07:00
|
|
|
return new DateTimeFormatterBuilder().appendChronologyText(style).toFormatter(locale).format(toTemporal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts this chronology to a {@code TemporalAccessor}.
|
|
|
|
* <p>
|
|
|
|
* A {@code Chronology} can be fully represented as a {@code TemporalAccessor}.
|
|
|
|
* However, the interface is not implemented by this class as most of the
|
|
|
|
* methods on the interface have no meaning to {@code Chronology}.
|
|
|
|
* <p>
|
|
|
|
* The returned temporal has no supported fields, with the query method
|
|
|
|
* supporting the return of the chronology using {@link TemporalQuery#chronology()}.
|
|
|
|
*
|
|
|
|
* @return a temporal equivalent to this chronology, not null
|
|
|
|
*/
|
|
|
|
private TemporalAccessor toTemporal() {
|
|
|
|
return new TemporalAccessor() {
|
2013-01-22 20:59:21 -08:00
|
|
|
@Override
|
|
|
|
public boolean isSupported(TemporalField field) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public long getLong(TemporalField field) {
|
2013-04-12 07:57:35 -07:00
|
|
|
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <R> R query(TemporalQuery<R> query) {
|
2013-04-12 07:57:35 -07:00
|
|
|
if (query == TemporalQuery.chronology()) {
|
2013-02-12 09:25:43 -08:00
|
|
|
return (R) Chronology.this;
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
return TemporalAccessor.super.query(query);
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Resolves parsed {@code ChronoField} values into a date during parsing.
|
|
|
|
* <p>
|
|
|
|
* Most {@code TemporalField} implementations are resolved using the
|
|
|
|
* resolve method on the field. By contrast, the {@code ChronoField} class
|
|
|
|
* defines fields that only have meaning relative to the chronology.
|
|
|
|
* As such, {@code ChronoField} date fields are resolved here in the
|
|
|
|
* context of a specific chronology.
|
|
|
|
* <p>
|
2013-07-18 11:02:02 -07:00
|
|
|
* {@code ChronoField} instances are resolved by this method, which may
|
|
|
|
* be overridden in subclasses.
|
|
|
|
* <ul>
|
|
|
|
* <li>{@code EPOCH_DAY} - If present, this is converted to a date and
|
|
|
|
* all other date fields are then cross-checked against the date.
|
|
|
|
* <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the
|
|
|
|
* {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart
|
|
|
|
* then the field is validated.
|
|
|
|
* <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they
|
|
|
|
* are combined to form a {@code YEAR}. In lenient mode, the {@code YEAR_OF_ERA}
|
|
|
|
* range is not validated, in smart and strict mode it is. The {@code ERA} is
|
|
|
|
* validated for range in all three modes. If only the {@code YEAR_OF_ERA} is
|
|
|
|
* present, and the mode is smart or lenient, then the last available era
|
|
|
|
* is assumed. In strict mode, no era is assumed and the {@code YEAR_OF_ERA} is
|
|
|
|
* left untouched. If only the {@code ERA} is present, then it is left untouched.
|
|
|
|
* <li>{@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} -
|
|
|
|
* If all three are present, then they are combined to form a date.
|
|
|
|
* In all three modes, the {@code YEAR} is validated.
|
|
|
|
* If the mode is smart or strict, then the month and day are validated.
|
|
|
|
* If the mode is lenient, then the date is combined in a manner equivalent to
|
|
|
|
* creating a date on the first day of the first month in the requested year,
|
|
|
|
* then adding the difference in months, then the difference in days.
|
|
|
|
* If the mode is smart, and the day-of-month is greater than the maximum for
|
|
|
|
* the year-month, then the day-of-month is adjusted to the last day-of-month.
|
|
|
|
* If the mode is strict, then the three fields must form a valid date.
|
|
|
|
* <li>{@code YEAR} and {@code DAY_OF_YEAR} -
|
|
|
|
* If both are present, then they are combined to form a date.
|
|
|
|
* In all three modes, the {@code YEAR} is validated.
|
|
|
|
* If the mode is lenient, then the date is combined in a manner equivalent to
|
|
|
|
* creating a date on the first day of the requested year, then adding
|
|
|
|
* the difference in days.
|
|
|
|
* If the mode is smart or strict, then the two fields must form a valid date.
|
|
|
|
* <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and
|
|
|
|
* {@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
|
|
|
|
* If all four are present, then they are combined to form a date.
|
|
|
|
* In all three modes, the {@code YEAR} is validated.
|
|
|
|
* If the mode is lenient, then the date is combined in a manner equivalent to
|
|
|
|
* creating a date on the first day of the first month in the requested year, then adding
|
|
|
|
* the difference in months, then the difference in weeks, then in days.
|
|
|
|
* If the mode is smart or strict, then the all four fields are validated to
|
|
|
|
* their outer ranges. The date is then combined in a manner equivalent to
|
|
|
|
* creating a date on the first day of the requested year and month, then adding
|
|
|
|
* the amount in weeks and days to reach their values. If the mode is strict,
|
|
|
|
* the date is additionally validated to check that the day and week adjustment
|
|
|
|
* did not change the month.
|
|
|
|
* <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and
|
|
|
|
* {@code DAY_OF_WEEK} - If all four are present, then they are combined to
|
|
|
|
* form a date. The approach is the same as described above for
|
|
|
|
* years, months and weeks in {@code ALIGNED_DAY_OF_WEEK_IN_MONTH}.
|
|
|
|
* The day-of-week is adjusted as the next or same matching day-of-week once
|
|
|
|
* the years, months and weeks have been handled.
|
|
|
|
* <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
|
|
|
|
* If all three are present, then they are combined to form a date.
|
|
|
|
* In all three modes, the {@code YEAR} is validated.
|
|
|
|
* If the mode is lenient, then the date is combined in a manner equivalent to
|
|
|
|
* creating a date on the first day of the requested year, then adding
|
|
|
|
* the difference in weeks, then in days.
|
|
|
|
* If the mode is smart or strict, then the all three fields are validated to
|
|
|
|
* their outer ranges. The date is then combined in a manner equivalent to
|
|
|
|
* creating a date on the first day of the requested year, then adding
|
|
|
|
* the amount in weeks and days to reach their values. If the mode is strict,
|
|
|
|
* the date is additionally validated to check that the day and week adjustment
|
|
|
|
* did not change the year.
|
|
|
|
* <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code DAY_OF_WEEK} -
|
|
|
|
* If all three are present, then they are combined to form a date.
|
|
|
|
* The approach is the same as described above for years and weeks in
|
|
|
|
* {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the
|
|
|
|
* next or same matching day-of-week once the years and weeks have been handled.
|
|
|
|
* </ul>
|
|
|
|
* <p>
|
2013-04-12 07:57:35 -07:00
|
|
|
* The default implementation is suitable for most calendar systems.
|
|
|
|
* If {@link ChronoField#YEAR_OF_ERA} is found without an {@link ChronoField#ERA}
|
|
|
|
* then the last era in {@link #eras()} is used.
|
|
|
|
* The implementation assumes a 7 day week, that the first day-of-month
|
2013-07-18 11:02:02 -07:00
|
|
|
* has the value 1, that first day-of-year has the value 1, and that the
|
|
|
|
* first of the month and year always exists.
|
2013-04-12 07:57:35 -07:00
|
|
|
*
|
|
|
|
* @param fieldValues the map of fields to values, which can be updated, not null
|
|
|
|
* @param resolverStyle the requested type of resolve, not null
|
|
|
|
* @return the resolved date, null if insufficient information to create a date
|
|
|
|
* @throws DateTimeException if the date cannot be resolved, typically
|
|
|
|
* because of a conflict in the input data
|
|
|
|
*/
|
2013-07-18 11:02:02 -07:00
|
|
|
public ChronoLocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
2013-04-12 07:57:35 -07:00
|
|
|
// check epoch-day before inventing era
|
|
|
|
if (fieldValues.containsKey(EPOCH_DAY)) {
|
|
|
|
return dateEpochDay(fieldValues.remove(EPOCH_DAY));
|
|
|
|
}
|
|
|
|
|
|
|
|
// fix proleptic month before inventing era
|
2013-07-18 11:02:02 -07:00
|
|
|
resolveProlepticMonth(fieldValues, resolverStyle);
|
2013-04-12 07:57:35 -07:00
|
|
|
|
|
|
|
// invent era if necessary to resolve year-of-era
|
2013-07-18 11:02:02 -07:00
|
|
|
ChronoLocalDate resolved = resolveYearOfEra(fieldValues, resolverStyle);
|
|
|
|
if (resolved != null) {
|
|
|
|
return resolved;
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// build date
|
|
|
|
if (fieldValues.containsKey(YEAR)) {
|
|
|
|
if (fieldValues.containsKey(MONTH_OF_YEAR)) {
|
|
|
|
if (fieldValues.containsKey(DAY_OF_MONTH)) {
|
2013-07-18 11:02:02 -07:00
|
|
|
return resolveYMD(fieldValues, resolverStyle);
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
if (fieldValues.containsKey(ALIGNED_WEEK_OF_MONTH)) {
|
|
|
|
if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_MONTH)) {
|
2013-07-18 11:02:02 -07:00
|
|
|
return resolveYMAA(fieldValues, resolverStyle);
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
if (fieldValues.containsKey(DAY_OF_WEEK)) {
|
2013-07-18 11:02:02 -07:00
|
|
|
return resolveYMAD(fieldValues, resolverStyle);
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fieldValues.containsKey(DAY_OF_YEAR)) {
|
2013-07-18 11:02:02 -07:00
|
|
|
return resolveYD(fieldValues, resolverStyle);
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
if (fieldValues.containsKey(ALIGNED_WEEK_OF_YEAR)) {
|
|
|
|
if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_YEAR)) {
|
2013-07-18 11:02:02 -07:00
|
|
|
return resolveYAA(fieldValues, resolverStyle);
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
if (fieldValues.containsKey(DAY_OF_WEEK)) {
|
2013-07-18 11:02:02 -07:00
|
|
|
return resolveYAD(fieldValues, resolverStyle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resolveProlepticMonth(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
Long pMonth = fieldValues.remove(PROLEPTIC_MONTH);
|
|
|
|
if (pMonth != null) {
|
|
|
|
if (resolverStyle != ResolverStyle.LENIENT) {
|
|
|
|
PROLEPTIC_MONTH.checkValidValue(pMonth);
|
|
|
|
}
|
|
|
|
// first day-of-month is likely to be safest for setting proleptic-month
|
|
|
|
// cannot add to year zero, as not all chronologies have a year zero
|
|
|
|
ChronoLocalDate chronoDate = dateNow()
|
|
|
|
.with(DAY_OF_MONTH, 1).with(PROLEPTIC_MONTH, pMonth);
|
|
|
|
addFieldValue(fieldValues, MONTH_OF_YEAR, chronoDate.get(MONTH_OF_YEAR));
|
|
|
|
addFieldValue(fieldValues, YEAR, chronoDate.get(YEAR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveYearOfEra(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
Long yoeLong = fieldValues.remove(YEAR_OF_ERA);
|
|
|
|
if (yoeLong != null) {
|
|
|
|
Long eraLong = fieldValues.remove(ERA);
|
|
|
|
int yoe;
|
|
|
|
if (resolverStyle != ResolverStyle.LENIENT) {
|
|
|
|
yoe = range(YEAR_OF_ERA).checkValidIntValue(yoeLong, YEAR_OF_ERA);
|
|
|
|
} else {
|
|
|
|
yoe = Math.toIntExact(yoeLong);
|
|
|
|
}
|
|
|
|
if (eraLong != null) {
|
|
|
|
Era eraObj = eraOf(range(ERA).checkValidIntValue(eraLong, ERA));
|
|
|
|
addFieldValue(fieldValues, YEAR, prolepticYear(eraObj, yoe));
|
|
|
|
} else {
|
|
|
|
if (fieldValues.containsKey(YEAR)) {
|
|
|
|
int year = range(YEAR).checkValidIntValue(fieldValues.get(YEAR), YEAR);
|
|
|
|
ChronoLocalDate chronoDate = dateYearDay(year, 1);
|
|
|
|
addFieldValue(fieldValues, YEAR, prolepticYear(chronoDate.getEra(), yoe));
|
|
|
|
} else if (resolverStyle == ResolverStyle.STRICT) {
|
|
|
|
// do not invent era if strict
|
|
|
|
// reinstate the field removed earlier, no cross-check issues
|
|
|
|
fieldValues.put(YEAR_OF_ERA, yoeLong);
|
|
|
|
} else {
|
|
|
|
List<Era> eras = eras();
|
|
|
|
if (eras.isEmpty()) {
|
|
|
|
addFieldValue(fieldValues, YEAR, yoe);
|
|
|
|
} else {
|
|
|
|
Era eraObj = eras.get(eras.size() - 1);
|
|
|
|
addFieldValue(fieldValues, YEAR, prolepticYear(eraObj, yoe));
|
|
|
|
}
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
}
|
2013-07-18 11:02:02 -07:00
|
|
|
} else if (fieldValues.containsKey(ERA)) {
|
|
|
|
range(ERA).checkValidValue(fieldValues.get(ERA), ERA); // always validated
|
2013-04-12 07:57:35 -07:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:02:02 -07:00
|
|
|
ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
|
|
|
|
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
|
|
long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
|
|
|
|
long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
|
|
|
|
return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);
|
|
|
|
}
|
|
|
|
int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
|
|
|
|
ValueRange domRange = range(DAY_OF_MONTH);
|
|
|
|
int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);
|
|
|
|
if (resolverStyle == ResolverStyle.SMART) { // previous valid
|
|
|
|
try {
|
|
|
|
return date(y, moy, dom);
|
|
|
|
} catch (DateTimeException ex) {
|
|
|
|
return date(y, moy, 1).with(TemporalAdjuster.lastDayOfMonth());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return date(y, moy, dom);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveYD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
|
|
|
|
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
|
|
long days = Math.subtractExact(fieldValues.remove(DAY_OF_YEAR), 1);
|
|
|
|
return dateYearDay(y, 1).plus(days, DAYS);
|
|
|
|
}
|
|
|
|
int doy = range(DAY_OF_YEAR).checkValidIntValue(fieldValues.remove(DAY_OF_YEAR), DAY_OF_YEAR);
|
|
|
|
return dateYearDay(y, doy); // smart is same as strict
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveYMAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
|
|
|
|
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
|
|
long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
|
|
|
|
long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);
|
|
|
|
long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), 1);
|
|
|
|
return date(y, 1, 1).plus(months, MONTHS).plus(weeks, WEEKS).plus(days, DAYS);
|
|
|
|
}
|
|
|
|
int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
|
|
|
|
int aw = range(ALIGNED_WEEK_OF_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), ALIGNED_WEEK_OF_MONTH);
|
|
|
|
int ad = range(ALIGNED_DAY_OF_WEEK_IN_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), ALIGNED_DAY_OF_WEEK_IN_MONTH);
|
|
|
|
ChronoLocalDate date = date(y, moy, 1).plus((aw - 1) * 7 + (ad - 1), DAYS);
|
|
|
|
if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {
|
|
|
|
throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");
|
|
|
|
}
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveYMAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
|
|
|
|
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
|
|
long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
|
|
|
|
long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);
|
|
|
|
long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1);
|
|
|
|
return resolveAligned(date(y, 1, 1), months, weeks, dow);
|
|
|
|
}
|
|
|
|
int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
|
|
|
|
int aw = range(ALIGNED_WEEK_OF_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), ALIGNED_WEEK_OF_MONTH);
|
|
|
|
int dow = range(DAY_OF_WEEK).checkValidIntValue(fieldValues.remove(DAY_OF_WEEK), DAY_OF_WEEK);
|
|
|
|
ChronoLocalDate date = date(y, moy, 1).plus((aw - 1) * 7, DAYS).with(nextOrSame(DayOfWeek.of(dow)));
|
|
|
|
if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {
|
|
|
|
throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");
|
|
|
|
}
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveYAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
|
|
|
|
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
|
|
long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);
|
|
|
|
long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), 1);
|
|
|
|
return dateYearDay(y, 1).plus(weeks, WEEKS).plus(days, DAYS);
|
|
|
|
}
|
|
|
|
int aw = range(ALIGNED_WEEK_OF_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), ALIGNED_WEEK_OF_YEAR);
|
|
|
|
int ad = range(ALIGNED_DAY_OF_WEEK_IN_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), ALIGNED_DAY_OF_WEEK_IN_YEAR);
|
|
|
|
ChronoLocalDate date = dateYearDay(y, 1).plus((aw - 1) * 7 + (ad - 1), DAYS);
|
|
|
|
if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {
|
|
|
|
throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
|
|
|
|
}
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveYAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
|
|
|
|
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
|
|
|
|
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
|
|
long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);
|
|
|
|
long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1);
|
|
|
|
return resolveAligned(dateYearDay(y, 1), 0, weeks, dow);
|
|
|
|
}
|
|
|
|
int aw = range(ALIGNED_WEEK_OF_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), ALIGNED_WEEK_OF_YEAR);
|
|
|
|
int dow = range(DAY_OF_WEEK).checkValidIntValue(fieldValues.remove(DAY_OF_WEEK), DAY_OF_WEEK);
|
|
|
|
ChronoLocalDate date = dateYearDay(y, 1).plus((aw - 1) * 7, DAYS).with(nextOrSame(DayOfWeek.of(dow)));
|
|
|
|
if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {
|
|
|
|
throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
|
|
|
|
}
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChronoLocalDate resolveAligned(ChronoLocalDate base, long months, long weeks, long dow) {
|
|
|
|
ChronoLocalDate date = base.plus(months, MONTHS).plus(weeks, WEEKS);
|
|
|
|
if (dow > 7) {
|
|
|
|
date = date.plus((dow - 1) / 7, WEEKS);
|
|
|
|
dow = ((dow - 1) % 7) + 1;
|
|
|
|
} else if (dow < 1) {
|
|
|
|
date = date.plus(Math.subtractExact(dow, 7) / 7, WEEKS);
|
|
|
|
dow = ((dow + 6) % 7) + 1;
|
|
|
|
}
|
|
|
|
return date.with(nextOrSame(DayOfWeek.of((int) dow)));
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:57:35 -07:00
|
|
|
/**
|
|
|
|
* Adds a field-value pair to the map, checking for conflicts.
|
|
|
|
* <p>
|
|
|
|
* If the field is not already present, then the field-value pair is added to the map.
|
|
|
|
* If the field is already present and it has the same value as that specified, no action occurs.
|
|
|
|
* If the field is already present and it has a different value to that specified, then
|
|
|
|
* an exception is thrown.
|
|
|
|
*
|
|
|
|
* @param field the field to add, not null
|
|
|
|
* @param value the value to add, not null
|
|
|
|
* @throws DateTimeException if the field is already present with a different value
|
|
|
|
*/
|
|
|
|
void addFieldValue(Map<TemporalField, Long> fieldValues, ChronoField field, long value) {
|
|
|
|
Long old = fieldValues.get(field); // check first for better error message
|
|
|
|
if (old != null && old.longValue() != value) {
|
|
|
|
throw new DateTimeException("Conflict found: " + field + " " + old + " differs from " + field + " " + value);
|
|
|
|
}
|
|
|
|
fieldValues.put(field, value);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Compares this chronology to another chronology.
|
|
|
|
* <p>
|
|
|
|
* The comparison order first by the chronology ID string, then by any
|
|
|
|
* additional information specific to the subclass.
|
|
|
|
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
|
|
* <p>
|
|
|
|
* The default implementation compares the chronology ID.
|
|
|
|
* Subclasses must compare any additional state that they store.
|
|
|
|
*
|
|
|
|
* @param other the other chronology to compare to, not null
|
|
|
|
* @return the comparator value, negative if less, positive if greater
|
|
|
|
*/
|
|
|
|
@Override
|
2013-02-12 09:25:43 -08:00
|
|
|
public int compareTo(Chronology other) {
|
2013-01-22 20:59:21 -08:00
|
|
|
return getId().compareTo(other.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this chronology is equal to another chronology.
|
|
|
|
* <p>
|
|
|
|
* The comparison is based on the entire state of the object.
|
|
|
|
* <p>
|
2013-02-12 09:25:43 -08:00
|
|
|
* The default implementation checks the type and calls {@link #compareTo(Chronology)}.
|
2013-01-22 20:59:21 -08:00
|
|
|
*
|
|
|
|
* @param obj the object to check, null returns false
|
|
|
|
* @return true if this is equal to the other chronology
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-12 09:25:43 -08:00
|
|
|
if (obj instanceof Chronology) {
|
|
|
|
return compareTo((Chronology) obj) == 0;
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hash code for this chronology.
|
|
|
|
* <p>
|
|
|
|
* The default implementation is based on the ID and class.
|
|
|
|
* Subclasses should add any additional state that they store.
|
|
|
|
*
|
|
|
|
* @return a suitable hash code
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return getClass().hashCode() ^ getId().hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Outputs this chronology as a {@code String}, using the ID.
|
|
|
|
*
|
|
|
|
* @return a string representation of this chronology, not null
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
/**
|
2013-04-12 07:57:35 -07:00
|
|
|
* Writes the Chronology using a
|
|
|
|
* <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
|
2013-09-11 10:16:21 -04:00
|
|
|
* @serialData
|
2013-01-22 20:59:21 -08:00
|
|
|
* <pre>
|
2013-09-11 10:16:21 -04:00
|
|
|
* out.writeByte(1); // identifies a Chronology
|
2013-04-12 07:57:35 -07:00
|
|
|
* out.writeUTF(getId());
|
2013-01-22 20:59:21 -08:00
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @return the instance of {@code Ser}, not null
|
|
|
|
*/
|
2013-09-11 10:16:21 -04:00
|
|
|
Object writeReplace() {
|
2013-01-22 20:59:21 -08:00
|
|
|
return new Ser(Ser.CHRONO_TYPE, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defend against malicious streams.
|
|
|
|
* @return never
|
|
|
|
* @throws InvalidObjectException always
|
|
|
|
*/
|
2013-09-11 10:16:21 -04:00
|
|
|
private Object readResolve() throws InvalidObjectException {
|
2013-01-22 20:59:21 -08:00
|
|
|
throw new InvalidObjectException("Deserialization via serialization delegate");
|
|
|
|
}
|
|
|
|
|
2013-09-11 10:16:21 -04:00
|
|
|
/**
|
|
|
|
* Write the Chronology id to the stream.
|
|
|
|
* @param out the output stream
|
|
|
|
* @throws IOException on any error during the write
|
|
|
|
*/
|
2013-01-22 20:59:21 -08:00
|
|
|
void writeExternal(DataOutput out) throws IOException {
|
|
|
|
out.writeUTF(getId());
|
|
|
|
}
|
|
|
|
|
2013-09-11 10:16:21 -04:00
|
|
|
/**
|
|
|
|
* Reads the Chronology id and creates the Chronology.
|
|
|
|
* @param in the input stream
|
|
|
|
* @return the Chronology
|
|
|
|
* @throws IOException on errors during the read
|
|
|
|
* @throws DateTimeException if the Chronology cannot be returned
|
|
|
|
*/
|
2013-02-12 09:25:43 -08:00
|
|
|
static Chronology readExternal(DataInput in) throws IOException {
|
2013-01-22 20:59:21 -08:00
|
|
|
String id = in.readUTF();
|
2013-02-12 09:25:43 -08:00
|
|
|
return Chronology.of(id);
|
2013-01-22 20:59:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|