src/share/classes/java/time/chrono/HijrahDate.java

Print this page

        

*** 52,62 **** * 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.calendar; 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; --- 52,62 ---- * 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 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;
*** 66,88 **** import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.Serializable; import java.time.DateTimeException; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.ChronoField; ! import java.time.temporal.ChronoLocalDate; import java.time.temporal.TemporalField; import java.time.temporal.ValueRange; import java.util.Objects; /** * A date in the Hijrah calendar system. * <p> ! * This implements {@code ChronoLocalDate} for the {@link HijrahChrono Hijrah calendar}. * <p> * The Hijrah calendar has a different total of days in a year than * Gregorian calendar, and a month is based on the period of a complete * revolution of the moon around the earth (as between successive new moons). * The calendar cycles becomes longer and unstable, and sometimes a manual --- 66,96 ---- import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.Serializable; + import java.time.Clock; import java.time.DateTimeException; import java.time.DayOfWeek; import java.time.LocalDate; + import java.time.LocalTime; + import java.time.Period; + import java.time.ZoneId; import java.time.temporal.ChronoField; ! import java.time.temporal.TemporalQuery; ! import java.time.temporal.TemporalAccessor; ! import java.time.temporal.TemporalAdjuster; ! import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalField; + import java.time.temporal.TemporalUnit; import java.time.temporal.ValueRange; import java.util.Objects; /** * A date in the Hijrah calendar system. * <p> ! * This date operates using the {@linkplain HijrahChronology Hijrah calendar}. * <p> * The Hijrah calendar has a different total of days in a year than * Gregorian calendar, and a month is based on the period of a complete * revolution of the moon around the earth (as between successive new moons). * The calendar cycles becomes longer and unstable, and sometimes a manual
*** 114,138 **** * <h3>Specification for implementors</h3> * This class is immutable and thread-safe. * * @since 1.8 */ ! final class HijrahDate ! extends ChronoDateImpl<HijrahChrono> ! implements ChronoLocalDate<HijrahChrono>, Serializable { ! // this class is package-scoped so that future conversion to public ! // would not change serialization /** * Serialization version. */ private static final long serialVersionUID = -5207853542612002020L; /** * The Chronology of this HijrahDate. */ ! private final HijrahChrono chrono; /** * The era. */ private final transient HijrahEra era; /** --- 122,144 ---- * <h3>Specification for implementors</h3> * This class is immutable and thread-safe. * * @since 1.8 */ ! public final class HijrahDate ! extends ChronoDateImpl<HijrahDate> ! implements ChronoLocalDate<HijrahDate>, Serializable { /** * Serialization version. */ private static final long serialVersionUID = -5207853542612002020L; /** * The Chronology of this HijrahDate. */ ! private final HijrahChronology chrono; /** * The era. */ private final transient HijrahEra era; /**
*** 175,185 **** * @param monthOfYear the month-of-year to represent, from 1 to 12 * @param dayOfMonth the day-of-month to represent, from 1 to 30 * @return the Hijrah date, never null * @throws DateTimeException if the value of any field is out of range */ ! static HijrahDate of(HijrahChrono chrono, int prolepticYear, int monthOfYear, int dayOfMonth) { return (prolepticYear >= 1) ? HijrahDate.of(chrono, HijrahEra.AH, prolepticYear, monthOfYear, dayOfMonth) : HijrahDate.of(chrono, HijrahEra.BEFORE_AH, 1 - prolepticYear, monthOfYear, dayOfMonth); } --- 181,191 ---- * @param monthOfYear the month-of-year to represent, from 1 to 12 * @param dayOfMonth the day-of-month to represent, from 1 to 30 * @return the Hijrah date, never null * @throws DateTimeException if the value of any field is out of range */ ! static HijrahDate of(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) { return (prolepticYear >= 1) ? HijrahDate.of(chrono, HijrahEra.AH, prolepticYear, monthOfYear, dayOfMonth) : HijrahDate.of(chrono, HijrahEra.BEFORE_AH, 1 - prolepticYear, monthOfYear, dayOfMonth); }
*** 192,232 **** * @param monthOfYear the month-of-year to represent, from 1 to 12 * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the Hijrah date, never null * @throws DateTimeException if the value of any field is out of range */ ! private static HijrahDate of(HijrahChrono chrono, HijrahEra era, int yearOfEra, int monthOfYear, int dayOfMonth) { Objects.requireNonNull(era, "era"); chrono.checkValidYearOfEra(yearOfEra); chrono.checkValidMonth(monthOfYear); chrono.checkValidDayOfMonth(dayOfMonth); long gregorianDays = chrono.getGregorianEpochDay(era.prolepticYear(yearOfEra), monthOfYear, dayOfMonth); return new HijrahDate(chrono, gregorianDays); } /** ! * Obtains an instance of {@code HijrahDate} from a date. * ! * @param date the date to use, not null ! * @return the Hijrah date, never null ! * @throws DateTimeException if the year is invalid */ ! private static HijrahDate of(HijrahChrono chrono, LocalDate date) { ! long gregorianDays = date.toEpochDay(); ! return new HijrahDate(chrono, gregorianDays); } ! static HijrahDate ofEpochDay(HijrahChrono chrono, long epochDay) { ! return new HijrahDate(chrono, epochDay); } /** * Constructs an instance with the specified date. * * @param gregorianDay the number of days from 0001/01/01 (Gregorian), caller calculated */ ! private HijrahDate(HijrahChrono chrono, long gregorianDay) { this.chrono = chrono; int[] dateInfo = chrono.getHijrahDateInfo(gregorianDay); chrono.checkValidYearOfEra(dateInfo[1]); chrono.checkValidMonth(dateInfo[2]); --- 198,313 ---- * @param monthOfYear the month-of-year to represent, from 1 to 12 * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the Hijrah date, never null * @throws DateTimeException if the value of any field is out of range */ ! private static HijrahDate of(HijrahChronology chrono, HijrahEra era, int yearOfEra, int monthOfYear, int dayOfMonth) { Objects.requireNonNull(era, "era"); chrono.checkValidYearOfEra(yearOfEra); chrono.checkValidMonth(monthOfYear); chrono.checkValidDayOfMonth(dayOfMonth); long gregorianDays = chrono.getGregorianEpochDay(era.prolepticYear(yearOfEra), monthOfYear, dayOfMonth); return new HijrahDate(chrono, gregorianDays); } + static HijrahDate ofEpochDay(HijrahChronology chrono, long epochDay) { + return new HijrahDate(chrono, epochDay); + } + + //----------------------------------------------------------------------- /** ! * Obtains the current {@code HijrahDate} 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. * ! * @return the current date using the system clock and default time-zone, not null */ ! public static HijrahDate now() { ! return now(Clock.systemDefaultZone()); } ! /** ! * Obtains the current {@code HijrahDate} 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 date using the system clock, not null ! */ ! public static HijrahDate now(ZoneId zone) { ! return now(Clock.system(zone)); ! } ! ! /** ! * Obtains the current {@code HijrahDate} 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 {@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 HijrahDate now(Clock clock) { ! return HijrahChronology.INSTANCE.date(LocalDate.now(clock)); } /** + * Obtains a {@code HijrahDate} representing a date in the Hijrah calendar + * system from the proleptic-year, month-of-year and day-of-month fields. + * <p> + * This returns a {@code HijrahDate} with the specified fields. + * The day must be valid for the year and month, otherwise an exception will be thrown. + * + * @param prolepticYear the Hijrah proleptic-year + * @param month the Hijrah month-of-year, from 1 to 12 + * @param dayOfMonth the Hijrah day-of-month, from 1 to 30 + * @return the date in Hijrah 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 HijrahDate of(int prolepticYear, int month, int dayOfMonth) { + return HijrahChronology.INSTANCE.date(prolepticYear, month, dayOfMonth); + } + + /** + * Obtains a {@code HijrahDate} from a temporal object. + * <p> + * This obtains a date in the Hijrah 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 HijrahDate}. + * <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 HijrahDate::from}. + * + * @param temporal the temporal object to convert, not null + * @return the date in Hijrah calendar system, not null + * @throws DateTimeException if unable to convert to a {@code HijrahDate} + */ + public static HijrahDate from(TemporalAccessor temporal) { + return HijrahChronology.INSTANCE.date(temporal); + } + + //----------------------------------------------------------------------- + /** * Constructs an instance with the specified date. * * @param gregorianDay the number of days from 0001/01/01 (Gregorian), caller calculated */ ! private HijrahDate(HijrahChronology chrono, long gregorianDay) { this.chrono = chrono; int[] dateInfo = chrono.getHijrahDateInfo(gregorianDay); chrono.checkValidYearOfEra(dateInfo[1]); chrono.checkValidMonth(dateInfo[2]);
*** 243,253 **** this.isLeapYear = chrono.isLeapYear(this.yearOfEra); } //----------------------------------------------------------------------- @Override ! public HijrahChrono getChrono() { return chrono; } @Override public ValueRange range(TemporalField field) { --- 324,334 ---- this.isLeapYear = chrono.isLeapYear(this.yearOfEra); } //----------------------------------------------------------------------- @Override ! public HijrahChronology getChronology() { return chrono; } @Override public ValueRange range(TemporalField field) {
*** 258,272 **** case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO case YEAR_OF_ERA: return ValueRange.of(1, 1000); // TODO } ! return getChrono().range(f); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.doRange(this); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { --- 339,358 ---- case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO case YEAR_OF_ERA: return ValueRange.of(1, 1000); // TODO } ! return getChronology().range(f); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.rangeRefinedBy(this); ! } ! ! @Override // Override for javadoc ! public int get(TemporalField field) { ! return super.get(field); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) {
*** 284,294 **** case YEAR: return yearOfEra; case ERA: return era.getValue(); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.doGet(this); } @Override public HijrahDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { --- 370,380 ---- case YEAR: return yearOfEra; case ERA: return era.getValue(); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.getFrom(this); } @Override public HijrahDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) {
*** 320,338 **** day = monthDays; } return HijrahDate.of(chrono, yearOfEra, month, day); } @Override ! public long toEpochDay() { ! return chrono.getGregorianEpochDay(yearOfEra, monthOfYear, dayOfMonth); } - //----------------------------------------------------------------------- @Override ! public HijrahEra getEra() { ! return this.era; } //----------------------------------------------------------------------- /** * Checks if the year is a leap year, according to the Hijrah calendar system rules. --- 406,448 ---- day = monthDays; } return HijrahDate.of(chrono, yearOfEra, month, day); } + /** + * {@inheritDoc} + * @throws DateTimeException {@inheritDoc} + * @throws ArithmeticException {@inheritDoc} + */ @Override ! public HijrahDate with(TemporalAdjuster adjuster) { ! return (HijrahDate)super.with(adjuster); ! } ! ! /** ! * {@inheritDoc} ! * @throws DateTimeException {@inheritDoc} ! * @throws ArithmeticException {@inheritDoc} ! */ ! @Override ! public HijrahDate plus(TemporalAmount amount) { ! return (HijrahDate)super.plus(amount); ! } ! ! /** ! * {@inheritDoc} ! * @throws DateTimeException {@inheritDoc} ! * @throws ArithmeticException {@inheritDoc} ! */ ! @Override ! public HijrahDate minus(TemporalAmount amount) { ! return (HijrahDate)super.minus(amount); } @Override ! public long toEpochDay() { ! return chrono.getGregorianEpochDay(yearOfEra, monthOfYear, dayOfMonth); } //----------------------------------------------------------------------- /** * Checks if the year is a leap year, according to the Hijrah calendar system rules.
*** 344,363 **** return this.isLeapYear; } //----------------------------------------------------------------------- @Override ! public HijrahDate plusYears(long years) { if (years == 0) { return this; } int newYear = Math.addExact(this.yearOfEra, (int)years); return HijrahDate.of(chrono, this.era, newYear, this.monthOfYear, this.dayOfMonth); } @Override ! public HijrahDate plusMonths(long months) { if (months == 0) { return this; } int newMonth = this.monthOfYear - 1; newMonth = newMonth + (int)months; --- 454,473 ---- return this.isLeapYear; } //----------------------------------------------------------------------- @Override ! HijrahDate plusYears(long years) { if (years == 0) { return this; } int newYear = Math.addExact(this.yearOfEra, (int)years); return HijrahDate.of(chrono, this.era, newYear, this.monthOfYear, this.dayOfMonth); } @Override ! HijrahDate plusMonths(long months) { if (months == 0) { return this; } int newMonth = this.monthOfYear - 1; newMonth = newMonth + (int)months;
*** 370,383 **** int newYear = Math.addExact(this.yearOfEra, years); return HijrahDate.of(chrono, this.era, newYear, newMonth + 1, this.dayOfMonth); } @Override ! public HijrahDate plusDays(long days) { return new HijrahDate(chrono, this.gregorianEpochDay + days); } /** * Returns month days from the beginning of year. * * @param month month (0-based) * @parma year year --- 480,528 ---- int newYear = Math.addExact(this.yearOfEra, years); return HijrahDate.of(chrono, this.era, newYear, newMonth + 1, this.dayOfMonth); } @Override ! HijrahDate plusWeeks(long weeksToAdd) { ! return (HijrahDate)super.plusWeeks(weeksToAdd); ! } ! ! @Override ! HijrahDate plusDays(long days) { return new HijrahDate(chrono, this.gregorianEpochDay + days); } + @Override + public HijrahDate plus(long amountToAdd, TemporalUnit unit) { + return (HijrahDate)super.plus(amountToAdd, unit); + } + + @Override + public HijrahDate minus(long amountToSubtract, TemporalUnit unit) { + return (HijrahDate)super.minus(amountToSubtract, unit); + } + + @Override + HijrahDate minusYears(long yearsToSubtract) { + return (HijrahDate)super.minusYears(yearsToSubtract); + } + + @Override + HijrahDate minusMonths(long monthsToSubtract) { + return (HijrahDate)super.minusMonths(monthsToSubtract); + } + + @Override + HijrahDate minusWeeks(long weeksToSubtract) { + return (HijrahDate)super.minusWeeks(weeksToSubtract); + } + + @Override + HijrahDate minusDays(long daysToSubtract) { + return (HijrahDate)super.minusDays(daysToSubtract); + } + /** * Returns month days from the beginning of year. * * @param month month (0-based) * @parma year year
*** 408,424 **** @Override public int lengthOfYear() { return chrono.getYearLength(yearOfEra); // TODO: proleptic year } //----------------------------------------------------------------------- private Object writeReplace() { return new Ser(Ser.HIJRAH_DATE_TYPE, this); } void writeExternal(ObjectOutput out) throws IOException { ! // HijrahChrono is implicit in the Hijrah_DATE_TYPE out.writeObject(chrono); out.writeInt(get(YEAR)); out.writeByte(get(MONTH_OF_YEAR)); out.writeByte(get(DAY_OF_MONTH)); } --- 553,593 ---- @Override public int lengthOfYear() { return chrono.getYearLength(yearOfEra); // TODO: proleptic year } + @Override // for javadoc and covariant return type + public final ChronoLocalDateTime<HijrahDate> atTime(LocalTime localTime) { + return (ChronoLocalDateTime<HijrahDate>)super.atTime(localTime); + } + + @Override + public Period periodUntil(ChronoLocalDate<?> endDate) { + // TODO: untested + HijrahDate end = (HijrahDate) getChronology().date(endDate); + long totalMonths = (end.yearOfEra - this.yearOfEra) * 12 + (end.monthOfYear - this.monthOfYear); // safe + int days = end.dayOfMonth - this.dayOfMonth; + if (totalMonths > 0 && days < 0) { + totalMonths--; + HijrahDate calcDate = this.plusMonths(totalMonths); + days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe + } else if (totalMonths < 0 && days > 0) { + totalMonths++; + days -= end.lengthOfMonth(); + } + long years = totalMonths / 12; // safe + int months = (int) (totalMonths % 12); // safe + return Period.of(Math.toIntExact(years), months, days); + } + //----------------------------------------------------------------------- private Object writeReplace() { return new Ser(Ser.HIJRAH_DATE_TYPE, this); } void writeExternal(ObjectOutput out) throws IOException { ! // HijrahChronology is implicit in the Hijrah_DATE_TYPE out.writeObject(chrono); out.writeInt(get(YEAR)); out.writeByte(get(MONTH_OF_YEAR)); out.writeByte(get(DAY_OF_MONTH)); }
*** 432,443 **** */ private Object readResolve() { return this; } ! static ChronoLocalDate<HijrahChrono> readExternal(ObjectInput in) throws IOException, ClassNotFoundException { ! HijrahChrono chrono = (HijrahChrono)in.readObject(); int year = in.readInt(); int month = in.readByte(); int dayOfMonth = in.readByte(); return chrono.date(year, month, dayOfMonth); } --- 601,612 ---- */ private Object readResolve() { return this; } ! static ChronoLocalDate<HijrahDate> readExternal(ObjectInput in) throws IOException, ClassNotFoundException { ! HijrahChronology chrono = (HijrahChronology)in.readObject(); int year = in.readInt(); int month = in.readByte(); int dayOfMonth = in.readByte(); return chrono.date(year, month, dayOfMonth); }