/* * Copyright (c) 2012, 2015, 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. */ package java.time.chrono; import java.io.InvalidObjectException; import static java.time.temporal.ChronoField.DAY_OF_MONTH; 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; import java.io.ObjectInputStream; import java.io.Serializable; import java.time.Clock; import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Month; import java.time.Period; import java.time.Year; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.ResolverStyle; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; import java.time.temporal.ValueRange; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; /** * The ISO calendar system. *

* This chronology defines the rules of the ISO calendar system. * This calendar system is based on the ISO-8601 standard, which is the * de facto world calendar. *

* The fields are defined as follows: *

* * @implSpec * This class is immutable and thread-safe. * * @since 1.8 */ public final class IsoChronology extends AbstractChronology implements Serializable { /** * Singleton instance of the ISO chronology. */ public static final IsoChronology INSTANCE = new IsoChronology(); /** * Serialization version. */ private static final long serialVersionUID = -1440403870442975015L; /** * Restricted constructor. */ private IsoChronology() { } //----------------------------------------------------------------------- /** * Gets the ID of the chronology - 'ISO'. *

* The ID uniquely identifies the {@code Chronology}. * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}. * * @return the chronology ID - 'ISO' * @see #getCalendarType() */ @Override public String getId() { return "ISO"; } /** * Gets the calendar type of the underlying calendar system - 'iso8601'. *

* The calendar type is an identifier defined by the * Unicode Locale Data Markup Language (LDML) specification. * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}. * It can also be used as part of a locale, accessible via * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'. * * @return the calendar system type - 'iso8601' * @see #getId() */ @Override public String getCalendarType() { return "iso8601"; } //----------------------------------------------------------------------- /** * Obtains an ISO local date from the era, year-of-era, month-of-year * and day-of-month fields. * * @param era the ISO era, not null * @param yearOfEra the ISO year-of-era * @param month the ISO month-of-year * @param dayOfMonth the ISO day-of-month * @return the ISO local date, not null * @throws DateTimeException if unable to create the date * @throws ClassCastException if the type of {@code era} is not {@code IsoEra} */ @Override // override with covariant return type public LocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) { return date(prolepticYear(era, yearOfEra), month, dayOfMonth); } /** * Obtains an ISO local date from the proleptic-year, month-of-year * and day-of-month fields. *

* This is equivalent to {@link LocalDate#of(int, int, int)}. * * @param prolepticYear the ISO proleptic-year * @param month the ISO month-of-year * @param dayOfMonth the ISO day-of-month * @return the ISO local date, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate date(int prolepticYear, int month, int dayOfMonth) { return LocalDate.of(prolepticYear, month, dayOfMonth); } /** * Obtains an ISO local date from the era, year-of-era and day-of-year fields. * * @param era the ISO era, not null * @param yearOfEra the ISO year-of-era * @param dayOfYear the ISO day-of-year * @return the ISO local date, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) { return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear); } /** * Obtains an ISO local date from the proleptic-year and day-of-year fields. *

* This is equivalent to {@link LocalDate#ofYearDay(int, int)}. * * @param prolepticYear the ISO proleptic-year * @param dayOfYear the ISO day-of-year * @return the ISO local date, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate dateYearDay(int prolepticYear, int dayOfYear) { return LocalDate.ofYearDay(prolepticYear, dayOfYear); } /** * Obtains an ISO local date from the epoch-day. *

* This is equivalent to {@link LocalDate#ofEpochDay(long)}. * * @param epochDay the epoch day * @return the ISO local date, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate dateEpochDay(long epochDay) { return LocalDate.ofEpochDay(epochDay); } //----------------------------------------------------------------------- /** * Obtains an ISO local date from another date-time object. *

* This is equivalent to {@link LocalDate#from(TemporalAccessor)}. * * @param temporal the date-time object to convert, not null * @return the ISO local date, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate date(TemporalAccessor temporal) { return LocalDate.from(temporal); } /** * Obtains an ISO local date-time from another date-time object. *

* This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}. * * @param temporal the date-time object to convert, not null * @return the ISO local date-time, not null * @throws DateTimeException if unable to create the date-time */ @Override // override with covariant return type public LocalDateTime localDateTime(TemporalAccessor temporal) { return LocalDateTime.from(temporal); } /** * Obtains an ISO zoned date-time from another date-time object. *

* This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}. * * @param temporal the date-time object to convert, not null * @return the ISO zoned date-time, not null * @throws DateTimeException if unable to create the date-time */ @Override // override with covariant return type public ZonedDateTime zonedDateTime(TemporalAccessor temporal) { return ZonedDateTime.from(temporal); } /** * Obtains an ISO zoned date-time in this chronology from an {@code Instant}. *

* This is equivalent to {@link ZonedDateTime#ofInstant(Instant, ZoneId)}. * * @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 */ @Override public ZonedDateTime zonedDateTime(Instant instant, ZoneId zone) { return ZonedDateTime.ofInstant(instant, zone); } //----------------------------------------------------------------------- /** * Obtains the current ISO local date from the system clock in the default time-zone. *

* This will query the {@link Clock#systemDefaultZone() system clock} in the default * time-zone to obtain the current date. *

* Using this method will prevent the ability to use an alternate clock for testing * because the clock is hard-coded. * * @return the current ISO local date using the system clock and default time-zone, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate dateNow() { return dateNow(Clock.systemDefaultZone()); } /** * Obtains the current ISO local date from the system clock in the specified time-zone. *

* 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. *

* Using this method will prevent the ability to use an alternate clock for testing * because the clock is hard-coded. * * @return the current ISO local date using the system clock, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate dateNow(ZoneId zone) { return dateNow(Clock.system(zone)); } /** * Obtains the current ISO local date from the specified clock. *

* 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 ISO local date, not null * @throws DateTimeException if unable to create the date */ @Override // override with covariant return type public LocalDate dateNow(Clock clock) { Objects.requireNonNull(clock, "clock"); return date(LocalDate.now(clock)); } //----------------------------------------------------------------------- /** * Checks if the year is a leap year, according to the ISO proleptic * calendar system rules. *

* This method applies the current rules for leap years across the whole time-line. * In general, a year is a leap year if it is divisible by four without * remainder. However, years divisible by 100, are not leap years, with * the exception of years divisible by 400 which are. *

* For example, 1904 is a leap year it is divisible by 4. * 1900 was not a leap year as it is divisible by 100, however 2000 was a * leap year as it is divisible by 400. *

* The calculation is proleptic - applying the same rules into the far future and far past. * This is historically inaccurate, but is correct for the ISO-8601 standard. * * @param prolepticYear the ISO proleptic year to check * @return true if the year is leap, false otherwise */ @Override public boolean isLeapYear(long prolepticYear) { return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0); } @Override public int prolepticYear(Era era, int yearOfEra) { if (era instanceof IsoEra == false) { throw new ClassCastException("Era must be IsoEra"); } return (era == IsoEra.CE ? yearOfEra : 1 - yearOfEra); } @Override public IsoEra eraOf(int eraValue) { return IsoEra.of(eraValue); } @Override public List eras() { return Arrays.asList(IsoEra.values()); } //----------------------------------------------------------------------- /** * Resolves parsed {@code ChronoField} values into a date during parsing. *

* 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. *

* {@code ChronoField} instances on the ISO calendar system are resolved * as follows. *

* * @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 */ @Override // override for performance public LocalDate resolveDate(Map fieldValues, ResolverStyle resolverStyle) { return (LocalDate) super.resolveDate(fieldValues, resolverStyle); } @Override // override for better proleptic algorithm void resolveProlepticMonth(Map fieldValues, ResolverStyle resolverStyle) { Long pMonth = fieldValues.remove(PROLEPTIC_MONTH); if (pMonth != null) { if (resolverStyle != ResolverStyle.LENIENT) { PROLEPTIC_MONTH.checkValidValue(pMonth); } addFieldValue(fieldValues, MONTH_OF_YEAR, Math.floorMod(pMonth, 12) + 1); addFieldValue(fieldValues, YEAR, Math.floorDiv(pMonth, 12)); } } @Override // override for enhanced behaviour LocalDate resolveYearOfEra(Map fieldValues, ResolverStyle resolverStyle) { Long yoeLong = fieldValues.remove(YEAR_OF_ERA); if (yoeLong != null) { if (resolverStyle != ResolverStyle.LENIENT) { YEAR_OF_ERA.checkValidValue(yoeLong); } Long era = fieldValues.remove(ERA); if (era == null) { Long year = fieldValues.get(YEAR); if (resolverStyle == ResolverStyle.STRICT) { // do not invent era if strict, but do cross-check with year if (year != null) { addFieldValue(fieldValues, YEAR, (year > 0 ? yoeLong: Math.subtractExact(1, yoeLong))); } else { // reinstate the field removed earlier, no cross-check issues fieldValues.put(YEAR_OF_ERA, yoeLong); } } else { // invent era addFieldValue(fieldValues, YEAR, (year == null || year > 0 ? yoeLong: Math.subtractExact(1, yoeLong))); } } else if (era.longValue() == 1L) { addFieldValue(fieldValues, YEAR, yoeLong); } else if (era.longValue() == 0L) { addFieldValue(fieldValues, YEAR, Math.subtractExact(1, yoeLong)); } else { throw new DateTimeException("Invalid value for era: " + era); } } else if (fieldValues.containsKey(ERA)) { ERA.checkValidValue(fieldValues.get(ERA)); // always validated } return null; } @Override // override for performance LocalDate resolveYMD(Map fieldValues, ResolverStyle resolverStyle) { int y = YEAR.checkValidIntValue(fieldValues.remove(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 LocalDate.of(y, 1, 1).plusMonths(months).plusDays(days); } int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR)); int dom = DAY_OF_MONTH.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH)); if (resolverStyle == ResolverStyle.SMART) { // previous valid if (moy == 4 || moy == 6 || moy == 9 || moy == 11) { dom = Math.min(dom, 30); } else if (moy == 2) { dom = Math.min(dom, Month.FEBRUARY.length(Year.isLeap(y))); } } return LocalDate.of(y, moy, dom); } //----------------------------------------------------------------------- @Override public ValueRange range(ChronoField field) { return field.range(); } //----------------------------------------------------------------------- /** * Obtains a period for this chronology based on years, months and days. *

* This returns a period tied to the ISO chronology using the specified * years, months and days. See {@link Period} for further details. * * @param years the number of years, may be negative * @param months the number of years, may be negative * @param days the number of years, may be negative * @return the period in terms of this chronology, not null * @return the ISO period, not null */ @Override // override with covariant return type public Period period(int years, int months, int days) { return Period.of(years, months, days); } //----------------------------------------------------------------------- /** * Writes the Chronology using a * dedicated serialized form. * @serialData *

     *  out.writeByte(1);     // identifies a Chronology
     *  out.writeUTF(getId());
     * 
* * @return the instance of {@code Ser}, not null */ @Override Object writeReplace() { return super.writeReplace(); } /** * Defend against malicious streams. * * @param s the stream to read * @throws InvalidObjectException always */ private void readObject(ObjectInputStream s) throws InvalidObjectException { throw new InvalidObjectException("Deserialization via serialization delegate"); } }