All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.topicus.jdbc.shaded.org.threeten.bp.chrono.JapaneseDate Maven / Gradle / Ivy

/*
 * Copyright (c) 2007-present, 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 nl.topicus.jdbc.shaded.org.threeten.bp.chrono;

import static nl.topicus.jdbc.shaded.org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH;
import static nl.topicus.jdbc.shaded.org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR;
import static nl.topicus.jdbc.shaded.org.threeten.bp.temporal.ChronoField.YEAR;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Calendar;

import nl.topicus.jdbc.shaded.org.threeten.bp.Clock;
import nl.topicus.jdbc.shaded.org.threeten.bp.DateTimeException;
import nl.topicus.jdbc.shaded.org.threeten.bp.LocalDate;
import nl.topicus.jdbc.shaded.org.threeten.bp.LocalTime;
import nl.topicus.jdbc.shaded.org.threeten.bp.Period;
import nl.topicus.jdbc.shaded.org.threeten.bp.ZoneId;
import nl.topicus.jdbc.shaded.org.threeten.bp.jdk8.Jdk8Methods;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.ChronoField;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.TemporalAccessor;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.TemporalAdjuster;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.TemporalAmount;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.TemporalField;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.TemporalQuery;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.TemporalUnit;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.UnsupportedTemporalTypeException;
import nl.topicus.jdbc.shaded.org.threeten.bp.temporal.ValueRange;

/**
 * A date in the Japanese Imperial calendar system.
 * 

* This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}. * This calendar system is primarily used in Japan. *

* The Japanese Imperial calendar system is the same as the ISO calendar system * apart from the era-based year numbering. The proleptic-year is defined to be * equal to the ISO proleptic-year. *

* Japan introduced the Gregorian calendar starting with Meiji 6. * Only Meiji and later eras are supported. *

* For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".
* Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.
* Calling {@code japaneseDate.get(YEAR)} will return 2012.
* Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to * {@code JapaneseChEra.HEISEI}.
* *

Specification for implementors

* This class is immutable and thread-safe. */ public final class JapaneseDate extends ChronoDateImpl implements Serializable { /** * Serialization version. */ private static final long serialVersionUID = -305327627230580483L; /** * Minimum date. */ static final LocalDate MIN_DATE = LocalDate.of(1873, 1, 1); /** * The underlying ISO local date. * @serial */ private final LocalDate isoDate; /** * The JapaneseEra of this date. */ private transient JapaneseEra era; /** * The Japanese imperial calendar year of this date. */ private transient int yearOfEra; //----------------------------------------------------------------------- /** * Obtains the current {@code JapaneseDate} 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 date using the system clock and default time-zone, not null */ public static JapaneseDate now() { return now(Clock.systemDefaultZone()); } /** * Obtains the current {@code JapaneseDate} 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. * * @param zone the zone ID to use, not null * @return the current date using the system clock, not null */ public static JapaneseDate now(ZoneId zone) { return now(Clock.system(zone)); } /** * Obtains the current {@code JapaneseDate} 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 {@linkplain Clock dependency injection}. * * @param clock the clock to use, not null * @return the current date, not null * @throws DateTimeException if the current date cannot be obtained */ public static JapaneseDate now(Clock clock) { return new JapaneseDate(LocalDate.now(clock)); } /** * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar * system from the era, year-of-era, month-of-year and day-of-month fields. *

* This returns a {@code JapaneseDate} with the specified fields. * The day must be valid for the year and month, otherwise an exception will be thrown. *

* The Japanese month and day-of-month are the same as those in the * ISO calendar system. They are not reset when the era changes. * For example: *

     *  6th Jan Showa 64 = ISO 1989-01-06
     *  7th Jan Showa 64 = ISO 1989-01-07
     *  8th Jan Heisei 1 = ISO 1989-01-08
     *  9th Jan Heisei 1 = ISO 1989-01-09
     * 
* * @param era the Japanese era, not null * @param yearOfEra the Japanese year-of-era * @param month the Japanese month-of-year, from 1 to 12 * @param dayOfMonth the Japanese day-of-month, from 1 to 31 * @return the date in Japanese calendar system, not null * @throws DateTimeException if the value of any field is out of range, * or if the day-of-month is invalid for the month-year */ public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) { Jdk8Methods.requireNonNull(era, "era"); if (yearOfEra < 1) { throw new DateTimeException("Invalid YearOfEra: " + yearOfEra); } LocalDate eraStartDate = era.startDate(); LocalDate eraEndDate = era.endDate(); int yearOffset = eraStartDate.getYear() - 1; LocalDate date = LocalDate.of(yearOfEra + yearOffset, month, dayOfMonth); if (date.isBefore(eraStartDate) || date.isAfter(eraEndDate)) { throw new DateTimeException("Requested date is outside bounds of era " + era); } return new JapaneseDate(era, yearOfEra, date); } /** * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar * system from the era, year-of-era and day-of-year fields. *

* This returns a {@code JapaneseDate} with the specified fields. * The day must be valid for the year, otherwise an exception will be thrown. * The Japanese day-of-year is reset when the era changes. * * @param era the Japanese era, not null * @param yearOfEra the Japanese year-of-era * @param dayOfYear the Japanese day-of-year, from 1 to 31 * @return the date in Japanese calendar system, not null * @throws DateTimeException if the value of any field is out of range, * or if the day-of-year is invalid for the year */ static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) { Jdk8Methods.requireNonNull(era, "era"); if (yearOfEra < 1) { throw new DateTimeException("Invalid YearOfEra: " + yearOfEra); } LocalDate eraStartDate = era.startDate(); LocalDate eraEndDate = era.endDate(); if (yearOfEra == 1) { dayOfYear += eraStartDate.getDayOfYear() - 1; if (dayOfYear > eraStartDate.lengthOfYear()) { throw new DateTimeException("DayOfYear exceeds maximum allowed in the first year of era " + era); } } int yearOffset = eraStartDate.getYear() - 1; LocalDate isoDate = LocalDate.ofYearDay(yearOfEra + yearOffset, dayOfYear); if (isoDate.isBefore(eraStartDate) || isoDate.isAfter(eraEndDate)) { throw new DateTimeException("Requested date is outside bounds of era " + era); } return new JapaneseDate(era, yearOfEra, isoDate); } /** * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar * system from the proleptic-year, month-of-year and day-of-month fields. *

* This returns a {@code JapaneseDate} with the specified fields. * The day must be valid for the year and month, otherwise an exception will be thrown. *

* The Japanese proleptic year, month and day-of-month are the same as those * in the ISO calendar system. They are not reset when the era changes. * * @param prolepticYear the Japanese proleptic-year * @param month the Japanese month-of-year, from 1 to 12 * @param dayOfMonth the Japanese day-of-month, from 1 to 31 * @return the date in Japanese calendar system, not null * @throws DateTimeException if the value of any field is out of range, * or if the day-of-month is invalid for the month-year */ public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) { return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth)); } /** * Obtains a {@code JapaneseDate} from a temporal object. *

* This obtains a date in the Japanese calendar system 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 JapaneseDate}. *

* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} * field, which is standardized across calendar systems. *

* This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code JapaneseDate::from}. * * @param temporal the temporal object to convert, not null * @return the date in Japanese calendar system, not null * @throws DateTimeException if unable to convert to a {@code JapaneseDate} */ public static JapaneseDate from(TemporalAccessor temporal) { return JapaneseChronology.INSTANCE.date(temporal); } //----------------------------------------------------------------------- /** * Creates an instance from an ISO date. * * @param isoDate the standard local date, validated not null */ JapaneseDate(LocalDate isoDate) { if (isoDate.isBefore(MIN_DATE)) { throw new DateTimeException("Minimum supported date is January 1st Meiji 6"); } this.era = JapaneseEra.from(isoDate); int yearOffset = this.era.startDate().getYear() - 1; this.yearOfEra = isoDate.getYear() - yearOffset; this.isoDate = isoDate; } /** * Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters, * and {@code era} and {@code year} must agree with {@code isoDate}. * * @param era the era, validated not null * @param year the year-of-era, validated * @param isoDate the standard local date, validated not null */ JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) { if (isoDate.isBefore(MIN_DATE)) { throw new DateTimeException("Minimum supported date is January 1st Meiji 6"); } this.era = era; this.yearOfEra = year; this.isoDate = isoDate; } /** * Reconstitutes this object from a stream. * * @param stream object input stream */ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); this.era = JapaneseEra.from(isoDate); int yearOffset = this.era.startDate().getYear() - 1; this.yearOfEra = isoDate.getYear() - yearOffset; } //----------------------------------------------------------------------- @Override public JapaneseChronology getChronology() { return JapaneseChronology.INSTANCE; } @Override public JapaneseEra getEra() { return era; } @Override public int lengthOfMonth() { return isoDate.lengthOfMonth(); } @Override public int lengthOfYear() { Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); return jcal.getActualMaximum(Calendar.DAY_OF_YEAR); } //----------------------------------------------------------------------- /** * Checks if the specified field is supported. *

* This checks if this date can be queried for the specified field. * If false, then calling the {@link #range(TemporalField) range} and * {@link #get(TemporalField) get} methods will throw an exception. *

* If the field is a {@link ChronoField} then the query is implemented here. * The supported fields are: *

    *
  • {@code DAY_OF_WEEK} *
  • {@code DAY_OF_MONTH} *
  • {@code DAY_OF_YEAR} *
  • {@code EPOCH_DAY} *
  • {@code MONTH_OF_YEAR} *
  • {@code PROLEPTIC_MONTH} *
  • {@code YEAR_OF_ERA} *
  • {@code YEAR} *
  • {@code ERA} *
* All other {@code ChronoField} instances will return false. *

* If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the field is supported is determined by the field. * * @param field the field to check, null returns false * @return true if the field is supported on this date, false if not */ @Override public boolean isSupported(TemporalField field) { if (field == ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH || field == ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR || field == ChronoField.ALIGNED_WEEK_OF_MONTH || field == ChronoField.ALIGNED_WEEK_OF_YEAR) { return false; } return super.isSupported(field); } @Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_YEAR: return actualRange(Calendar.DAY_OF_YEAR); case YEAR_OF_ERA: return actualRange(Calendar.YEAR); } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); } private ValueRange actualRange(int calendarField) { Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); return ValueRange.of(jcal.getActualMinimum(calendarField), jcal.getActualMaximum(calendarField)); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case ALIGNED_DAY_OF_WEEK_IN_MONTH: case ALIGNED_DAY_OF_WEEK_IN_YEAR: case ALIGNED_WEEK_OF_MONTH: case ALIGNED_WEEK_OF_YEAR: throw new UnsupportedTemporalTypeException("Unsupported field: " + field); case YEAR_OF_ERA: return yearOfEra; case ERA: return era.getValue(); case DAY_OF_YEAR: return getDayOfYear(); } return isoDate.getLong(field); } return field.getFrom(this); } private long getDayOfYear() { if (yearOfEra == 1) { return isoDate.getDayOfYear() - era.startDate().getDayOfYear() + 1; } return isoDate.getDayOfYear(); } //----------------------------------------------------------------------- @Override public JapaneseDate with(TemporalAdjuster adjuster) { return (JapaneseDate) super.with(adjuster); } @Override public JapaneseDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (getLong(f) == newValue) { // validates unsupported fields return this; } switch (f) { case DAY_OF_YEAR: case YEAR_OF_ERA: case ERA: { int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); switch (f) { case DAY_OF_YEAR: return with(isoDate.plusDays(nvalue - getDayOfYear())); case YEAR_OF_ERA: return this.withYear(nvalue); case ERA: { return this.withYear(JapaneseEra.of(nvalue), yearOfEra); } } } } return with(isoDate.with(field, newValue)); } return field.adjustInto(this, newValue); } @Override public JapaneseDate plus(TemporalAmount amount) { return (JapaneseDate) super.plus(amount); } @Override public JapaneseDate plus(long amountToAdd, TemporalUnit unit) { return (JapaneseDate) super.plus(amountToAdd, unit); } @Override public JapaneseDate minus(TemporalAmount amount) { return (JapaneseDate) super.minus(amount); } @Override public JapaneseDate minus(long amountToAdd, TemporalUnit unit) { return (JapaneseDate) super.minus(amountToAdd, unit); } //----------------------------------------------------------------------- /** * Returns a copy of this date with the year altered. *

* This method changes the year of the date. * If the month-day is invalid for the year, then the previous valid day * will be selected instead. *

* This instance is immutable and unaffected by this method call. * * @param era the era to set in the result, not null * @param yearOfEra the year-of-era to set in the returned date * @return a {@code JapaneseDate} based on this date with the requested year, never null * @throws DateTimeException if {@code year} is invalid */ private JapaneseDate withYear(JapaneseEra era, int yearOfEra) { int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra); return with(isoDate.withYear(year)); } /** * Returns a copy of this date with the year-of-era altered. *

* This method changes the year-of-era of the date. * If the month-day is invalid for the year, then the previous valid day * will be selected instead. *

* This instance is immutable and unaffected by this method call. * * @param year the year to set in the returned date * @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null * @throws DateTimeException if {@code year} is invalid */ private JapaneseDate withYear(int year) { return withYear(getEra(), year); } //----------------------------------------------------------------------- @Override JapaneseDate plusYears(long years) { return with(isoDate.plusYears(years)); } @Override JapaneseDate plusMonths(long months) { return with(isoDate.plusMonths(months)); } @Override JapaneseDate plusDays(long days) { return with(isoDate.plusDays(days)); } private JapaneseDate with(LocalDate newDate) { return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate)); } @Override @SuppressWarnings("unchecked") public final ChronoLocalDateTime atTime(LocalTime localTime) { return (ChronoLocalDateTime)super.atTime(localTime); } @Override public ChronoPeriod until(ChronoLocalDate endDate) { Period period = isoDate.until(endDate); return getChronology().period(period.getYears(), period.getMonths(), period.getDays()); } @Override // override for performance public long toEpochDay() { return isoDate.toEpochDay(); } //------------------------------------------------------------------------- @Override // override for performance public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof JapaneseDate) { JapaneseDate otherDate = (JapaneseDate) obj; return this.isoDate.equals(otherDate.isoDate); } return false; } @Override // override for performance public int hashCode() { return getChronology().getId().hashCode() ^ isoDate.hashCode(); } //----------------------------------------------------------------------- private Object writeReplace() { return new Ser(Ser.JAPANESE_DATE_TYPE, this); } void writeExternal(DataOutput out) throws IOException { // JapaneseChrono is implicit in the JAPANESE_DATE_TYPE out.writeInt(get(YEAR)); out.writeByte(get(MONTH_OF_YEAR)); out.writeByte(get(DAY_OF_MONTH)); } static ChronoLocalDate readExternal(DataInput in) throws IOException { int year = in.readInt(); int month = in.readByte(); int dayOfMonth = in.readByte(); return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy