1 /* 2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 package java.time.chrono; 58 59 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 60 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 61 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 62 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 63 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 65 import static java.time.temporal.ChronoField.YEAR; 66 67 import java.io.IOException; 68 import java.io.InvalidObjectException; 69 import java.io.ObjectInput; 70 import java.io.ObjectInputStream; 71 import java.io.ObjectOutput; 72 import java.io.Serializable; 73 import java.time.Clock; 74 import java.time.DateTimeException; 75 import java.time.LocalDate; 76 import java.time.LocalTime; 77 import java.time.ZoneId; 78 import java.time.temporal.ChronoField; 79 import java.time.temporal.TemporalAccessor; 80 import java.time.temporal.TemporalAdjuster; 81 import java.time.temporal.TemporalAmount; 82 import java.time.temporal.TemporalField; 83 import java.time.temporal.TemporalQuery; 84 import java.time.temporal.TemporalUnit; 85 import java.time.temporal.UnsupportedTemporalTypeException; 86 import java.time.temporal.ValueRange; 87 88 // Android-changed: removed ValueBased paragraph. 89 /** 90 * A date in the Hijrah calendar system. 91 * <p> 92 * This date operates using one of several variants of the 93 * {@linkplain HijrahChronology Hijrah calendar}. 94 * <p> 95 * The Hijrah calendar has a different total of days in a year than 96 * Gregorian calendar, and the length of each month is based on the period 97 * of a complete revolution of the moon around the earth 98 * (as between successive new moons). 99 * Refer to the {@link HijrahChronology} for details of supported variants. 100 * <p> 101 * Each HijrahDate is created bound to a particular HijrahChronology, 102 * The same chronology is propagated to each HijrahDate computed from the date. 103 * To use a different Hijrah variant, its HijrahChronology can be used 104 * to create new HijrahDate instances. 105 * Alternatively, the {@link #withVariant} method can be used to convert 106 * to a new HijrahChronology. 107 * 108 * @implSpec 109 * This class is immutable and thread-safe. 110 * 111 * @since 1.8 112 */ 113 public final class HijrahDate 114 extends ChronoLocalDateImpl<HijrahDate> 115 implements ChronoLocalDate, Serializable { 116 117 /** 118 * Serialization version. 119 */ 120 @java.io.Serial 121 private static final long serialVersionUID = -5207853542612002020L; 122 /** 123 * The Chronology of this HijrahDate. 124 */ 125 private final transient HijrahChronology chrono; 126 /** 127 * The proleptic year. 128 */ 129 private final transient int prolepticYear; 130 /** 131 * The month-of-year. 132 */ 133 private final transient int monthOfYear; 134 /** 135 * The day-of-month. 136 */ 137 private final transient int dayOfMonth; 138 139 //------------------------------------------------------------------------- 140 /** 141 * Obtains an instance of {@code HijrahDate} from the Hijrah proleptic year, 142 * month-of-year and day-of-month. 143 * 144 * @param prolepticYear the proleptic year to represent in the Hijrah calendar 145 * @param monthOfYear the month-of-year to represent, from 1 to 12 146 * @param dayOfMonth the day-of-month to represent, from 1 to 30 147 * @return the Hijrah date, never null 148 * @throws DateTimeException if the value of any field is out of range 149 */ of(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth)150 static HijrahDate of(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) { 151 return new HijrahDate(chrono, prolepticYear, monthOfYear, dayOfMonth); 152 } 153 154 /** 155 * Returns a HijrahDate for the chronology and epochDay. 156 * @param chrono The Hijrah chronology 157 * @param epochDay the epoch day 158 * @return a HijrahDate for the epoch day; non-null 159 */ ofEpochDay(HijrahChronology chrono, long epochDay)160 static HijrahDate ofEpochDay(HijrahChronology chrono, long epochDay) { 161 return new HijrahDate(chrono, epochDay); 162 } 163 164 //----------------------------------------------------------------------- 165 /** 166 * Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar 167 * in the default time-zone. 168 * <p> 169 * This will query the {@link Clock#systemDefaultZone() system clock} in the default 170 * time-zone to obtain the current date. 171 * <p> 172 * Using this method will prevent the ability to use an alternate clock for testing 173 * because the clock is hard-coded. 174 * 175 * @return the current date using the system clock and default time-zone, not null 176 */ now()177 public static HijrahDate now() { 178 return now(Clock.systemDefaultZone()); 179 } 180 181 /** 182 * Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar 183 * in the specified time-zone. 184 * <p> 185 * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. 186 * Specifying the time-zone avoids dependence on the default time-zone. 187 * <p> 188 * Using this method will prevent the ability to use an alternate clock for testing 189 * because the clock is hard-coded. 190 * 191 * @param zone the zone ID to use, not null 192 * @return the current date using the system clock, not null 193 */ now(ZoneId zone)194 public static HijrahDate now(ZoneId zone) { 195 return now(Clock.system(zone)); 196 } 197 198 /** 199 * Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar 200 * from the specified clock. 201 * <p> 202 * This will query the specified clock to obtain the current date - today. 203 * Using this method allows the use of an alternate clock for testing. 204 * The alternate clock may be introduced using {@linkplain Clock dependency injection}. 205 * 206 * @param clock the clock to use, not null 207 * @return the current date, not null 208 * @throws DateTimeException if the current date cannot be obtained 209 */ now(Clock clock)210 public static HijrahDate now(Clock clock) { 211 return HijrahDate.ofEpochDay(HijrahChronology.INSTANCE, LocalDate.now(clock).toEpochDay()); 212 } 213 214 /** 215 * Obtains a {@code HijrahDate} of the Islamic Umm Al-Qura calendar 216 * from the proleptic-year, month-of-year and day-of-month fields. 217 * <p> 218 * This returns a {@code HijrahDate} with the specified fields. 219 * The day must be valid for the year and month, otherwise an exception will be thrown. 220 * 221 * @param prolepticYear the Hijrah proleptic-year 222 * @param month the Hijrah month-of-year, from 1 to 12 223 * @param dayOfMonth the Hijrah day-of-month, from 1 to 30 224 * @return the date in Hijrah calendar system, not null 225 * @throws DateTimeException if the value of any field is out of range, 226 * or if the day-of-month is invalid for the month-year 227 */ of(int prolepticYear, int month, int dayOfMonth)228 public static HijrahDate of(int prolepticYear, int month, int dayOfMonth) { 229 return HijrahChronology.INSTANCE.date(prolepticYear, month, dayOfMonth); 230 } 231 232 /** 233 * Obtains a {@code HijrahDate} of the Islamic Umm Al-Qura calendar from a temporal object. 234 * <p> 235 * This obtains a date in the Hijrah calendar system based on the specified temporal. 236 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, 237 * which this factory converts to an instance of {@code HijrahDate}. 238 * <p> 239 * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} 240 * field, which is standardized across calendar systems. 241 * <p> 242 * This method matches the signature of the functional interface {@link TemporalQuery} 243 * allowing it to be used as a query via method reference, {@code HijrahDate::from}. 244 * 245 * @param temporal the temporal object to convert, not null 246 * @return the date in Hijrah calendar system, not null 247 * @throws DateTimeException if unable to convert to a {@code HijrahDate} 248 */ from(TemporalAccessor temporal)249 public static HijrahDate from(TemporalAccessor temporal) { 250 return HijrahChronology.INSTANCE.date(temporal); 251 } 252 253 //----------------------------------------------------------------------- 254 /** 255 * Constructs an {@code HijrahDate} with the proleptic-year, month-of-year and 256 * day-of-month fields. 257 * 258 * @param chrono The chronology to create the date with 259 * @param prolepticYear the proleptic year 260 * @param monthOfYear the month of year 261 * @param dayOfMonth the day of month 262 */ HijrahDate(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth)263 private HijrahDate(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) { 264 // Computing the Gregorian day checks the valid ranges 265 chrono.getEpochDay(prolepticYear, monthOfYear, dayOfMonth); 266 267 this.chrono = chrono; 268 this.prolepticYear = prolepticYear; 269 this.monthOfYear = monthOfYear; 270 this.dayOfMonth = dayOfMonth; 271 } 272 273 /** 274 * Constructs an instance with the Epoch Day. 275 * 276 * @param epochDay the epochDay 277 */ HijrahDate(HijrahChronology chrono, long epochDay)278 private HijrahDate(HijrahChronology chrono, long epochDay) { 279 int[] dateInfo = chrono.getHijrahDateInfo((int)epochDay); 280 281 this.chrono = chrono; 282 this.prolepticYear = dateInfo[0]; 283 this.monthOfYear = dateInfo[1]; 284 this.dayOfMonth = dateInfo[2]; 285 } 286 287 //----------------------------------------------------------------------- 288 /** 289 * Gets the chronology of this date, which is the Hijrah calendar system. 290 * <p> 291 * The {@code Chronology} represents the calendar system in use. 292 * The era and other fields in {@link ChronoField} are defined by the chronology. 293 * 294 * @return the Hijrah chronology, not null 295 */ 296 @Override getChronology()297 public HijrahChronology getChronology() { 298 return chrono; 299 } 300 301 /** 302 * Gets the era applicable at this date. 303 * <p> 304 * The Hijrah calendar system has one era, 'AH', 305 * defined by {@link HijrahEra}. 306 * 307 * @return the era applicable at this date, not null 308 */ 309 @Override getEra()310 public HijrahEra getEra() { 311 return HijrahEra.AH; 312 } 313 314 /** 315 * Returns the length of the month represented by this date. 316 * <p> 317 * This returns the length of the month in days. 318 * Month lengths in the Hijrah calendar system vary between 29 and 30 days. 319 * 320 * @return the length of the month in days 321 */ 322 @Override lengthOfMonth()323 public int lengthOfMonth() { 324 return chrono.getMonthLength(prolepticYear, monthOfYear); 325 } 326 327 /** 328 * Returns the length of the year represented by this date. 329 * <p> 330 * This returns the length of the year in days. 331 * A Hijrah calendar system year is typically shorter than 332 * that of the ISO calendar system. 333 * 334 * @return the length of the year in days 335 */ 336 @Override lengthOfYear()337 public int lengthOfYear() { 338 return chrono.getYearLength(prolepticYear); 339 } 340 341 //----------------------------------------------------------------------- 342 @Override range(TemporalField field)343 public ValueRange range(TemporalField field) { 344 if (field instanceof ChronoField) { 345 if (isSupported(field)) { 346 ChronoField f = (ChronoField) field; 347 switch (f) { 348 case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); 349 case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); 350 case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO 351 // TODO does the limited range of valid years cause years to 352 // start/end part way through? that would affect range 353 } 354 return getChronology().range(f); 355 } 356 throw new UnsupportedTemporalTypeException("Unsupported field: " + field); 357 } 358 return field.rangeRefinedBy(this); 359 } 360 361 @Override getLong(TemporalField field)362 public long getLong(TemporalField field) { 363 if (field instanceof ChronoField) { 364 switch ((ChronoField) field) { 365 case DAY_OF_WEEK: return getDayOfWeek(); 366 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((dayOfMonth - 1) % 7) + 1; 367 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; 368 case DAY_OF_MONTH: return this.dayOfMonth; 369 case DAY_OF_YEAR: return this.getDayOfYear(); 370 case EPOCH_DAY: return toEpochDay(); 371 case ALIGNED_WEEK_OF_MONTH: return ((dayOfMonth - 1) / 7) + 1; 372 case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; 373 case MONTH_OF_YEAR: return monthOfYear; 374 case PROLEPTIC_MONTH: return getProlepticMonth(); 375 case YEAR_OF_ERA: return prolepticYear; 376 case YEAR: return prolepticYear; 377 case ERA: return getEraValue(); 378 } 379 throw new UnsupportedTemporalTypeException("Unsupported field: " + field); 380 } 381 return field.getFrom(this); 382 } 383 getProlepticMonth()384 private long getProlepticMonth() { 385 return prolepticYear * 12L + monthOfYear - 1; 386 } 387 388 @Override with(TemporalField field, long newValue)389 public HijrahDate with(TemporalField field, long newValue) { 390 if (field instanceof ChronoField chronoField) { 391 // not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work 392 chrono.range(chronoField).checkValidValue(newValue, chronoField); // TODO: validate value 393 int nvalue = (int) newValue; 394 switch (chronoField) { 395 case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek()); 396 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH)); 397 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR)); 398 case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, monthOfYear, nvalue); 399 case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear()); 400 case EPOCH_DAY: return new HijrahDate(chrono, newValue); 401 case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7); 402 case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7); 403 case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth); 404 case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth()); 405 case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth); 406 case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth); 407 case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth); 408 } 409 throw new UnsupportedTemporalTypeException("Unsupported field: " + field); 410 } 411 return super.with(field, newValue); 412 } 413 resolvePreviousValid(int prolepticYear, int month, int day)414 private HijrahDate resolvePreviousValid(int prolepticYear, int month, int day) { 415 int monthDays = chrono.getMonthLength(prolepticYear, month); 416 if (day > monthDays) { 417 day = monthDays; 418 } 419 return HijrahDate.of(chrono, prolepticYear, month, day); 420 } 421 422 /** 423 * {@inheritDoc} 424 * @throws DateTimeException if unable to make the adjustment. 425 * For example, if the adjuster requires an ISO chronology 426 * @throws ArithmeticException {@inheritDoc} 427 */ 428 @Override with(TemporalAdjuster adjuster)429 public HijrahDate with(TemporalAdjuster adjuster) { 430 return super.with(adjuster); 431 } 432 433 /** 434 * Returns a {@code HijrahDate} with the Chronology requested. 435 * <p> 436 * The year, month, and day are checked against the new requested 437 * HijrahChronology. If the chronology has a shorter month length 438 * for the month, the day is reduced to be the last day of the month. 439 * 440 * @param chronology the new HijrahChonology, non-null 441 * @return a HijrahDate with the requested HijrahChronology, non-null 442 */ withVariant(HijrahChronology chronology)443 public HijrahDate withVariant(HijrahChronology chronology) { 444 if (chrono == chronology) { 445 return this; 446 } 447 // Like resolvePreviousValid the day is constrained to stay in the same month 448 int monthDays = chronology.getDayOfYear(prolepticYear, monthOfYear); 449 return HijrahDate.of(chronology, prolepticYear, monthOfYear,(dayOfMonth > monthDays) ? monthDays : dayOfMonth ); 450 } 451 452 /** 453 * {@inheritDoc} 454 * @throws DateTimeException {@inheritDoc} 455 * @throws ArithmeticException {@inheritDoc} 456 */ 457 @Override plus(TemporalAmount amount)458 public HijrahDate plus(TemporalAmount amount) { 459 return super.plus(amount); 460 } 461 462 /** 463 * {@inheritDoc} 464 * @throws DateTimeException {@inheritDoc} 465 * @throws ArithmeticException {@inheritDoc} 466 */ 467 @Override minus(TemporalAmount amount)468 public HijrahDate minus(TemporalAmount amount) { 469 return super.minus(amount); 470 } 471 472 @Override toEpochDay()473 public long toEpochDay() { 474 return chrono.getEpochDay(prolepticYear, monthOfYear, dayOfMonth); 475 } 476 477 /** 478 * Gets the day-of-year field. 479 * <p> 480 * This method returns the primitive {@code int} value for the day-of-year. 481 * 482 * @return the day-of-year 483 */ getDayOfYear()484 private int getDayOfYear() { 485 return chrono.getDayOfYear(prolepticYear, monthOfYear) + dayOfMonth; 486 } 487 488 /** 489 * Gets the day-of-week value. 490 * 491 * @return the day-of-week; computed from the epochday 492 */ getDayOfWeek()493 private int getDayOfWeek() { 494 int dow0 = Math.floorMod(toEpochDay() + 3, 7); 495 return dow0 + 1; 496 } 497 498 /** 499 * Gets the Era of this date. 500 * 501 * @return the Era of this date; computed from epochDay 502 */ getEraValue()503 private int getEraValue() { 504 return (prolepticYear > 1 ? 1 : 0); 505 } 506 507 //----------------------------------------------------------------------- 508 /** 509 * Checks if the year is a leap year, according to the Hijrah calendar system rules. 510 * 511 * @return true if this date is in a leap year 512 */ 513 @Override isLeapYear()514 public boolean isLeapYear() { 515 return chrono.isLeapYear(prolepticYear); 516 } 517 518 //----------------------------------------------------------------------- 519 @Override plusYears(long years)520 HijrahDate plusYears(long years) { 521 if (years == 0) { 522 return this; 523 } 524 int newYear = Math.addExact(this.prolepticYear, (int)years); 525 return resolvePreviousValid(newYear, monthOfYear, dayOfMonth); 526 } 527 528 @Override plusMonths(long monthsToAdd)529 HijrahDate plusMonths(long monthsToAdd) { 530 if (monthsToAdd == 0) { 531 return this; 532 } 533 long monthCount = prolepticYear * 12L + (monthOfYear - 1); 534 long calcMonths = monthCount + monthsToAdd; // safe overflow 535 int newYear = chrono.checkValidYear(Math.floorDiv(calcMonths, 12L)); 536 int newMonth = (int)Math.floorMod(calcMonths, 12L) + 1; 537 return resolvePreviousValid(newYear, newMonth, dayOfMonth); 538 } 539 540 @Override plusWeeks(long weeksToAdd)541 HijrahDate plusWeeks(long weeksToAdd) { 542 return super.plusWeeks(weeksToAdd); 543 } 544 545 @Override plusDays(long days)546 HijrahDate plusDays(long days) { 547 return new HijrahDate(chrono, toEpochDay() + days); 548 } 549 550 @Override plus(long amountToAdd, TemporalUnit unit)551 public HijrahDate plus(long amountToAdd, TemporalUnit unit) { 552 return super.plus(amountToAdd, unit); 553 } 554 555 @Override minus(long amountToSubtract, TemporalUnit unit)556 public HijrahDate minus(long amountToSubtract, TemporalUnit unit) { 557 return super.minus(amountToSubtract, unit); 558 } 559 560 @Override minusYears(long yearsToSubtract)561 HijrahDate minusYears(long yearsToSubtract) { 562 return super.minusYears(yearsToSubtract); 563 } 564 565 @Override minusMonths(long monthsToSubtract)566 HijrahDate minusMonths(long monthsToSubtract) { 567 return super.minusMonths(monthsToSubtract); 568 } 569 570 @Override minusWeeks(long weeksToSubtract)571 HijrahDate minusWeeks(long weeksToSubtract) { 572 return super.minusWeeks(weeksToSubtract); 573 } 574 575 @Override minusDays(long daysToSubtract)576 HijrahDate minusDays(long daysToSubtract) { 577 return super.minusDays(daysToSubtract); 578 } 579 580 @Override // for javadoc and covariant return type 581 @SuppressWarnings("unchecked") atTime(LocalTime localTime)582 public final ChronoLocalDateTime<HijrahDate> atTime(LocalTime localTime) { 583 return (ChronoLocalDateTime<HijrahDate>)super.atTime(localTime); 584 } 585 586 @Override until(ChronoLocalDate endDate)587 public ChronoPeriod until(ChronoLocalDate endDate) { 588 // TODO: untested 589 HijrahDate end = getChronology().date(endDate); 590 long totalMonths = (end.prolepticYear - this.prolepticYear) * 12 + (end.monthOfYear - this.monthOfYear); // safe 591 int days = end.dayOfMonth - this.dayOfMonth; 592 if (totalMonths > 0 && days < 0) { 593 totalMonths--; 594 HijrahDate calcDate = this.plusMonths(totalMonths); 595 days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe 596 } else if (totalMonths < 0 && days > 0) { 597 totalMonths++; 598 days -= end.lengthOfMonth(); 599 } 600 long years = totalMonths / 12; // safe 601 int months = (int) (totalMonths % 12); // safe 602 return getChronology().period(Math.toIntExact(years), months, days); 603 } 604 605 //------------------------------------------------------------------------- 606 /** 607 * Compares this date to another date, including the chronology. 608 * <p> 609 * Compares this {@code HijrahDate} with another ensuring that the date is the same. 610 * <p> 611 * Only objects of type {@code HijrahDate} are compared, other types return false. 612 * To compare the dates of two {@code TemporalAccessor} instances, including dates 613 * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator. 614 * 615 * @param obj the object to check, null returns false 616 * @return true if this is equal to the other date and the Chronologies are equal 617 */ 618 @Override // override for performance equals(Object obj)619 public boolean equals(Object obj) { 620 if (this == obj) { 621 return true; 622 } 623 return (obj instanceof HijrahDate otherDate) 624 && prolepticYear == otherDate.prolepticYear 625 && this.monthOfYear == otherDate.monthOfYear 626 && this.dayOfMonth == otherDate.dayOfMonth 627 && getChronology().equals(otherDate.getChronology()); 628 } 629 630 /** 631 * A hash code for this date. 632 * 633 * @return a suitable hash code based only on the Chronology and the date 634 */ 635 @Override // override for performance hashCode()636 public int hashCode() { 637 int yearValue = prolepticYear; 638 int monthValue = monthOfYear; 639 int dayValue = dayOfMonth; 640 return getChronology().getId().hashCode() ^ (yearValue & 0xFFFFF800) 641 ^ ((yearValue << 11) + (monthValue << 6) + (dayValue)); 642 } 643 644 //----------------------------------------------------------------------- 645 /** 646 * Defend against malicious streams. 647 * 648 * @param s the stream to read 649 * @throws InvalidObjectException always 650 */ 651 @java.io.Serial readObject(ObjectInputStream s)652 private void readObject(ObjectInputStream s) throws InvalidObjectException { 653 throw new InvalidObjectException("Deserialization via serialization delegate"); 654 } 655 656 /** 657 * Writes the object using a 658 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>. 659 * @serialData 660 * <pre> 661 * out.writeByte(6); // identifies a HijrahDate 662 * out.writeObject(chrono); // the HijrahChronology variant 663 * out.writeInt(get(YEAR)); 664 * out.writeByte(get(MONTH_OF_YEAR)); 665 * out.writeByte(get(DAY_OF_MONTH)); 666 * </pre> 667 * 668 * @return the instance of {@code Ser}, not null 669 */ 670 @java.io.Serial writeReplace()671 private Object writeReplace() { 672 return new Ser(Ser.HIJRAH_DATE_TYPE, this); 673 } 674 writeExternal(ObjectOutput out)675 void writeExternal(ObjectOutput out) throws IOException { 676 // HijrahChronology is implicit in the Hijrah_DATE_TYPE 677 out.writeObject(getChronology()); 678 out.writeInt(get(YEAR)); 679 out.writeByte(get(MONTH_OF_YEAR)); 680 out.writeByte(get(DAY_OF_MONTH)); 681 } 682 readExternal(ObjectInput in)683 static HijrahDate readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 684 HijrahChronology chrono = (HijrahChronology) in.readObject(); 685 int year = in.readInt(); 686 int month = in.readByte(); 687 int dayOfMonth = in.readByte(); 688 return chrono.date(year, month, dayOfMonth); 689 } 690 691 } 692