1 /* 2 * Copyright (c) 2012, 2016, 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 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos 33 * 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions are met: 38 * 39 * * Redistributions of source code must retain the above copyright notice, 40 * this list of conditions and the following disclaimer. 41 * 42 * * Redistributions in binary form must reproduce the above copyright notice, 43 * this list of conditions and the following disclaimer in the documentation 44 * and/or other materials provided with the distribution. 45 * 46 * * Neither the name of JSR-310 nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time.chrono; 63 64 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 65 import static java.time.temporal.ChronoField.ERA; 66 import static java.time.temporal.ChronoField.HOUR_OF_DAY; 67 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; 68 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 69 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH; 70 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; 71 import static java.time.temporal.ChronoField.YEAR; 72 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 73 74 import java.io.InvalidObjectException; 75 import java.io.ObjectInputStream; 76 import java.io.Serializable; 77 import java.time.Clock; 78 import java.time.DateTimeException; 79 import java.time.Instant; 80 import java.time.LocalDate; 81 import java.time.LocalDateTime; 82 import java.time.Month; 83 import java.time.Period; 84 import java.time.Year; 85 import java.time.ZonedDateTime; 86 import java.time.ZoneId; 87 import java.time.ZoneOffset; 88 import java.time.format.ResolverStyle; 89 import java.time.temporal.ChronoField; 90 import java.time.temporal.TemporalAccessor; 91 import java.time.temporal.TemporalField; 92 import java.time.temporal.ValueRange; 93 import java.util.Arrays; 94 import java.util.List; 95 import java.util.Locale; 96 import java.util.Map; 97 import java.util.Objects; 98 99 /** 100 * The ISO calendar system. 101 * <p> 102 * This chronology defines the rules of the ISO calendar system. 103 * This calendar system is based on the ISO-8601 standard, which is the 104 * <i>de facto</i> world calendar. 105 * <p> 106 * The fields are defined as follows: 107 * <ul> 108 * <li>era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE). 109 * <li>year-of-era - The year-of-era is the same as the proleptic-year for the current CE era. 110 * For the BCE era before the ISO epoch the year increases from 1 upwards as time goes backwards. 111 * <li>proleptic-year - The proleptic year is the same as the year-of-era for the 112 * current era. For the previous era, years have zero, then negative values. 113 * <li>month-of-year - There are 12 months in an ISO year, numbered from 1 to 12. 114 * <li>day-of-month - There are between 28 and 31 days in each of the ISO month, numbered from 1 to 31. 115 * Months 4, 6, 9 and 11 have 30 days, Months 1, 3, 5, 7, 8, 10 and 12 have 31 days. 116 * Month 2 has 28 days, or 29 in a leap year. 117 * <li>day-of-year - There are 365 days in a standard ISO year and 366 in a leap year. 118 * The days are numbered from 1 to 365 or 1 to 366. 119 * <li>leap-year - Leap years occur every 4 years, except where the year is divisble by 100 and not divisble by 400. 120 * </ul> 121 * 122 * @implSpec 123 * This class is immutable and thread-safe. 124 * 125 * @since 1.8 126 */ 127 public final class IsoChronology extends AbstractChronology implements Serializable { 128 129 /** 130 * Singleton instance of the ISO chronology. 131 */ 132 public static final IsoChronology INSTANCE = new IsoChronology(); 133 134 /** 135 * Serialization version. 136 */ 137 private static final long serialVersionUID = -1440403870442975015L; 138 139 /** 140 * Restricted constructor. 141 */ 142 private IsoChronology() { 143 } 144 145 //----------------------------------------------------------------------- 146 /** 147 * Gets the ID of the chronology - 'ISO'. 148 * <p> 149 * The ID uniquely identifies the {@code Chronology}. 150 * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}. 151 * 152 * @return the chronology ID - 'ISO' 153 * @see #getCalendarType() 154 */ 155 @Override 156 public String getId() { 157 return "ISO"; 158 } 159 160 /** 161 * Gets the calendar type of the underlying calendar system - 'iso8601'. 162 * <p> 163 * The calendar type is an identifier defined by the 164 * <em>Unicode Locale Data Markup Language (LDML)</em> specification. 165 * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}. 166 * It can also be used as part of a locale, accessible via 167 * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'. 168 * 169 * @return the calendar system type - 'iso8601' 170 * @see #getId() 171 */ 172 @Override 173 public String getCalendarType() { 174 return "iso8601"; 175 } 176 177 //----------------------------------------------------------------------- 178 /** 179 * Obtains an ISO local date from the era, year-of-era, month-of-year 180 * and day-of-month fields. 181 * 182 * @param era the ISO era, not null 183 * @param yearOfEra the ISO year-of-era 184 * @param month the ISO month-of-year 185 * @param dayOfMonth the ISO day-of-month 186 * @return the ISO local date, not null 187 * @throws DateTimeException if unable to create the date 188 * @throws ClassCastException if the type of {@code era} is not {@code IsoEra} 189 */ 190 @Override // override with covariant return type 191 public LocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) { 192 return date(prolepticYear(era, yearOfEra), month, dayOfMonth); 193 } 194 195 /** 196 * Obtains an ISO local date from the proleptic-year, month-of-year 197 * and day-of-month fields. 198 * <p> 199 * This is equivalent to {@link LocalDate#of(int, int, int)}. 200 * 201 * @param prolepticYear the ISO proleptic-year 202 * @param month the ISO month-of-year 203 * @param dayOfMonth the ISO day-of-month 204 * @return the ISO local date, not null 205 * @throws DateTimeException if unable to create the date 206 */ 207 @Override // override with covariant return type 208 public LocalDate date(int prolepticYear, int month, int dayOfMonth) { 209 return LocalDate.of(prolepticYear, month, dayOfMonth); 210 } 211 212 /** 213 * Obtains an ISO local date from the era, year-of-era and day-of-year fields. 214 * 215 * @param era the ISO era, not null 216 * @param yearOfEra the ISO year-of-era 217 * @param dayOfYear the ISO day-of-year 218 * @return the ISO local date, not null 219 * @throws DateTimeException if unable to create the date 220 */ 221 @Override // override with covariant return type 222 public LocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) { 223 return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear); 224 } 225 226 /** 227 * Obtains an ISO local date from the proleptic-year and day-of-year fields. 228 * <p> 229 * This is equivalent to {@link LocalDate#ofYearDay(int, int)}. 230 * 231 * @param prolepticYear the ISO proleptic-year 232 * @param dayOfYear the ISO day-of-year 233 * @return the ISO local date, not null 234 * @throws DateTimeException if unable to create the date 235 */ 236 @Override // override with covariant return type 237 public LocalDate dateYearDay(int prolepticYear, int dayOfYear) { 238 return LocalDate.ofYearDay(prolepticYear, dayOfYear); 239 } 240 241 /** 242 * Obtains an ISO local date from the epoch-day. 243 * <p> 244 * This is equivalent to {@link LocalDate#ofEpochDay(long)}. 245 * 246 * @param epochDay the epoch day 247 * @return the ISO local date, not null 248 * @throws DateTimeException if unable to create the date 249 */ 250 @Override // override with covariant return type 251 public LocalDate dateEpochDay(long epochDay) { 252 return LocalDate.ofEpochDay(epochDay); 253 } 254 255 //----------------------------------------------------------------------- 256 /** 257 * Obtains an ISO local date from another date-time object. 258 * <p> 259 * This is equivalent to {@link LocalDate#from(TemporalAccessor)}. 260 * 261 * @param temporal the date-time object to convert, not null 262 * @return the ISO local date, not null 263 * @throws DateTimeException if unable to create the date 264 */ 265 @Override // override with covariant return type 266 public LocalDate date(TemporalAccessor temporal) { 267 return LocalDate.from(temporal); 268 } 269 270 //----------------------------------------------------------------------- 271 /** 272 * Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z 273 * formed using the specified year, month, day, hour, minute, second and zoneOffset. 274 * 275 * @param prolepticYear the year to represent, from MIN_YEAR to MAX_YEAR 276 * @param month the month-of-year to represent, from 1 to 12 277 * @param dayOfMonth the day-of-month to represent, from 1 to 31 278 * @param hour the hour-of-day to represent, from 0 to 23 279 * @param minute the minute-of-hour to represent, from 0 to 59 280 * @param second the second-of-minute to represent, from 0 to 59 281 * @param zoneOffset the zone offset, not null 282 * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative 283 * @throws DateTimeException if the value of any field is out of range, 284 * or if the day-of-month is invalid for the month-year 285 */ 286 @Override 287 public long epochSecond(int prolepticYear, int month, int dayOfMonth, 288 int hour, int minute, int second, ZoneOffset zoneOffset) { 289 YEAR.checkValidValue(prolepticYear); 290 MONTH_OF_YEAR.checkValidValue(month); 291 DAY_OF_MONTH.checkValidValue(dayOfMonth); 292 HOUR_OF_DAY.checkValidValue(hour); 293 MINUTE_OF_HOUR.checkValidValue(minute); 294 SECOND_OF_MINUTE.checkValidValue(second); 295 Objects.requireNonNull(zoneOffset, "zoneOffset"); 296 if (dayOfMonth > 28) { 297 int dom = numberOfDaysOfMonth(prolepticYear, month); 298 if (dayOfMonth > dom) { 299 if (dayOfMonth == 29) { 300 throw new DateTimeException("Invalid date 'February 29' as '" + prolepticYear + "' is not a leap year"); 301 } else { 302 throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'"); 303 } 304 } 305 } 306 307 long totalDays = 0; 308 int timeinSec = 0; 309 totalDays += 365L * prolepticYear; 310 long DAYS_0000_TO_1970 = (146097 * 5L) - (30L * 365L + 7L); // taken from LocalDate 311 if (prolepticYear >= 0) { 312 totalDays += (prolepticYear + 3L) / 4 - (prolepticYear + 99L) / 100 + (prolepticYear + 399L) / 400; 313 } else { 314 totalDays -= prolepticYear / -4 - prolepticYear / -100 + prolepticYear / -400; 315 } 316 totalDays += (367 * month - 362) / 12; 317 totalDays += dayOfMonth - 1; 318 if (month > 2) { 319 totalDays--; 320 if (IsoChronology.INSTANCE.isLeapYear(prolepticYear) == false) { 321 totalDays--; 322 } 323 } 324 totalDays -= DAYS_0000_TO_1970; 325 timeinSec = (hour * 60 + minute ) * 60 + second; 326 return Math.addExact(Math.multiplyExact(totalDays, 86400L), timeinSec - zoneOffset.getTotalSeconds()); 327 } 328 329 /** 330 * Gets the number of days for the given month in the given year. 331 * 332 * @param year the year to represent, from MIN_YEAR to MAX_YEAR 333 * @param month the month-of-year to represent, from 1 to 12 334 * @return the number of days for the given month in the given year 335 */ 336 private int numberOfDaysOfMonth(int year, int month) { 337 int dom; 338 switch (month) { 339 case 2: 340 dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28); 341 break; 342 case 4: 343 case 6: 344 case 9: 345 case 11: 346 dom = 30; 347 break; 348 default: 349 dom = 31; 350 break; 351 } 352 return dom; 353 } 354 355 //----------------------------------------------------------------------- 356 /** 357 * Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z 358 * formed using the specified era, yearOfEra, month, day, hour, minute, second and zoneOffset. 359 * 360 * @param era the ISO era, not null 361 * @param yearOfEra the ISO year-of-era 362 * @param month the ISO month-of-year 363 * @param dayOfMonth the ISO day-of-month 364 * @param hour the hour-of-day to represent, from 0 to 23 365 * @param minute the minute-of-hour to represent, from 0 to 59 366 * @param second the second-of-minute to represent, from 0 to 59 367 * @param zoneOffset the zone offset, not null 368 * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative 369 * @throws DateTimeException if the value of any field is out of range, 370 * or if the day-of-month is invalid for the month-year 371 */ 372 @Override 373 public long epochSecond(Era era, 374 int yearOfEra, int month, int dayOfMonth, 375 int hour, int minute, int second, ZoneOffset zoneOffset) { 376 Objects.requireNonNull(era, "era"); 377 if (era.getValue() == 0) { 378 yearOfEra = 1 - yearOfEra; 379 } 380 return epochSecond(yearOfEra, month, dayOfMonth, hour, minute, second, zoneOffset); 381 } 382 383 /** 384 * Obtains an ISO local date-time from another date-time object. 385 * <p> 386 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}. 387 * 388 * @param temporal the date-time object to convert, not null 389 * @return the ISO local date-time, not null 390 * @throws DateTimeException if unable to create the date-time 391 */ 392 @Override // override with covariant return type 393 public LocalDateTime localDateTime(TemporalAccessor temporal) { 394 return LocalDateTime.from(temporal); 395 } 396 397 /** 398 * Obtains an ISO zoned date-time from another date-time object. 399 * <p> 400 * This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}. 401 * 402 * @param temporal the date-time object to convert, not null 403 * @return the ISO zoned date-time, not null 404 * @throws DateTimeException if unable to create the date-time 405 */ 406 @Override // override with covariant return type 407 public ZonedDateTime zonedDateTime(TemporalAccessor temporal) { 408 return ZonedDateTime.from(temporal); 409 } 410 411 /** 412 * Obtains an ISO zoned date-time in this chronology from an {@code Instant}. 413 * <p> 414 * This is equivalent to {@link ZonedDateTime#ofInstant(Instant, ZoneId)}. 415 * 416 * @param instant the instant to create the date-time from, not null 417 * @param zone the time-zone, not null 418 * @return the zoned date-time, not null 419 * @throws DateTimeException if the result exceeds the supported range 420 */ 421 @Override 422 public ZonedDateTime zonedDateTime(Instant instant, ZoneId zone) { 423 return ZonedDateTime.ofInstant(instant, zone); 424 } 425 426 //----------------------------------------------------------------------- 427 /** 428 * Obtains the current ISO local date from the system clock in the default time-zone. 429 * <p> 430 * This will query the {@link Clock#systemDefaultZone() system clock} in the default 431 * time-zone to obtain the current date. 432 * <p> 433 * Using this method will prevent the ability to use an alternate clock for testing 434 * because the clock is hard-coded. 435 * 436 * @return the current ISO local date using the system clock and default time-zone, not null 437 * @throws DateTimeException if unable to create the date 438 */ 439 @Override // override with covariant return type 440 public LocalDate dateNow() { 441 return dateNow(Clock.systemDefaultZone()); 442 } 443 444 /** 445 * Obtains the current ISO local date from the system clock in the specified time-zone. 446 * <p> 447 * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. 448 * Specifying the time-zone avoids dependence on the default time-zone. 449 * <p> 450 * Using this method will prevent the ability to use an alternate clock for testing 451 * because the clock is hard-coded. 452 * 453 * @return the current ISO local date using the system clock, not null 454 * @throws DateTimeException if unable to create the date 455 */ 456 @Override // override with covariant return type 457 public LocalDate dateNow(ZoneId zone) { 458 return dateNow(Clock.system(zone)); 459 } 460 461 /** 462 * Obtains the current ISO local date from the specified clock. 463 * <p> 464 * This will query the specified clock to obtain the current date - today. 465 * Using this method allows the use of an alternate clock for testing. 466 * The alternate clock may be introduced using {@link Clock dependency injection}. 467 * 468 * @param clock the clock to use, not null 469 * @return the current ISO local date, not null 470 * @throws DateTimeException if unable to create the date 471 */ 472 @Override // override with covariant return type 473 public LocalDate dateNow(Clock clock) { 474 Objects.requireNonNull(clock, "clock"); 475 return date(LocalDate.now(clock)); 476 } 477 478 //----------------------------------------------------------------------- 479 /** 480 * Checks if the year is a leap year, according to the ISO proleptic 481 * calendar system rules. 482 * <p> 483 * This method applies the current rules for leap years across the whole time-line. 484 * In general, a year is a leap year if it is divisible by four without 485 * remainder. However, years divisible by 100, are not leap years, with 486 * the exception of years divisible by 400 which are. 487 * <p> 488 * For example, 1904 is a leap year it is divisible by 4. 489 * 1900 was not a leap year as it is divisible by 100, however 2000 was a 490 * leap year as it is divisible by 400. 491 * <p> 492 * The calculation is proleptic - applying the same rules into the far future and far past. 493 * This is historically inaccurate, but is correct for the ISO-8601 standard. 494 * 495 * @param prolepticYear the ISO proleptic year to check 496 * @return true if the year is leap, false otherwise 497 */ 498 @Override 499 public boolean isLeapYear(long prolepticYear) { 500 return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0); 501 } 502 503 @Override 504 public int prolepticYear(Era era, int yearOfEra) { 505 if (era instanceof IsoEra == false) { 506 throw new ClassCastException("Era must be IsoEra"); 507 } 508 return (era == IsoEra.CE ? yearOfEra : 1 - yearOfEra); 509 } 510 511 @Override 512 public IsoEra eraOf(int eraValue) { 513 return IsoEra.of(eraValue); 514 } 515 516 @Override 517 public List<Era> eras() { 518 return Arrays.<Era>asList(IsoEra.values()); 519 } 520 521 //----------------------------------------------------------------------- 522 /** 523 * Resolves parsed {@code ChronoField} values into a date during parsing. 524 * <p> 525 * Most {@code TemporalField} implementations are resolved using the 526 * resolve method on the field. By contrast, the {@code ChronoField} class 527 * defines fields that only have meaning relative to the chronology. 528 * As such, {@code ChronoField} date fields are resolved here in the 529 * context of a specific chronology. 530 * <p> 531 * {@code ChronoField} instances on the ISO calendar system are resolved 532 * as follows. 533 * <ul> 534 * <li>{@code EPOCH_DAY} - If present, this is converted to a {@code LocalDate} 535 * and all other date fields are then cross-checked against the date. 536 * <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the 537 * {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart 538 * then the field is validated. 539 * <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they 540 * are combined to form a {@code YEAR}. In lenient mode, the {@code YEAR_OF_ERA} 541 * range is not validated, in smart and strict mode it is. The {@code ERA} is 542 * validated for range in all three modes. If only the {@code YEAR_OF_ERA} is 543 * present, and the mode is smart or lenient, then the current era (CE/AD) 544 * is assumed. In strict mode, no era is assumed and the {@code YEAR_OF_ERA} is 545 * left untouched. If only the {@code ERA} is present, then it is left untouched. 546 * <li>{@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} - 547 * If all three are present, then they are combined to form a {@code LocalDate}. 548 * In all three modes, the {@code YEAR} is validated. If the mode is smart or strict, 549 * then the month and day are validated, with the day validated from 1 to 31. 550 * If the mode is lenient, then the date is combined in a manner equivalent to 551 * creating a date on the first of January in the requested year, then adding 552 * the difference in months, then the difference in days. 553 * If the mode is smart, and the day-of-month is greater than the maximum for 554 * the year-month, then the day-of-month is adjusted to the last day-of-month. 555 * If the mode is strict, then the three fields must form a valid date. 556 * <li>{@code YEAR} and {@code DAY_OF_YEAR} - 557 * If both are present, then they are combined to form a {@code LocalDate}. 558 * In all three modes, the {@code YEAR} is validated. 559 * If the mode is lenient, then the date is combined in a manner equivalent to 560 * creating a date on the first of January in the requested year, then adding 561 * the difference in days. 562 * If the mode is smart or strict, then the two fields must form a valid date. 563 * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and 564 * {@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - 565 * If all four are present, then they are combined to form a {@code LocalDate}. 566 * In all three modes, the {@code YEAR} is validated. 567 * If the mode is lenient, then the date is combined in a manner equivalent to 568 * creating a date on the first of January in the requested year, then adding 569 * the difference in months, then the difference in weeks, then in days. 570 * If the mode is smart or strict, then the all four fields are validated to 571 * their outer ranges. The date is then combined in a manner equivalent to 572 * creating a date on the first day of the requested year and month, then adding 573 * the amount in weeks and days to reach their values. If the mode is strict, 574 * the date is additionally validated to check that the day and week adjustment 575 * did not change the month. 576 * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and 577 * {@code DAY_OF_WEEK} - If all four are present, then they are combined to 578 * form a {@code LocalDate}. The approach is the same as described above for 579 * years, months and weeks in {@code ALIGNED_DAY_OF_WEEK_IN_MONTH}. 580 * The day-of-week is adjusted as the next or same matching day-of-week once 581 * the years, months and weeks have been handled. 582 * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - 583 * If all three are present, then they are combined to form a {@code LocalDate}. 584 * In all three modes, the {@code YEAR} is validated. 585 * If the mode is lenient, then the date is combined in a manner equivalent to 586 * creating a date on the first of January in the requested year, then adding 587 * the difference in weeks, then in days. 588 * If the mode is smart or strict, then the all three fields are validated to 589 * their outer ranges. The date is then combined in a manner equivalent to 590 * creating a date on the first day of the requested year, then adding 591 * the amount in weeks and days to reach their values. If the mode is strict, 592 * the date is additionally validated to check that the day and week adjustment 593 * did not change the year. 594 * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code DAY_OF_WEEK} - 595 * If all three are present, then they are combined to form a {@code LocalDate}. 596 * The approach is the same as described above for years and weeks in 597 * {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the 598 * next or same matching day-of-week once the years and weeks have been handled. 599 * </ul> 600 * 601 * @param fieldValues the map of fields to values, which can be updated, not null 602 * @param resolverStyle the requested type of resolve, not null 603 * @return the resolved date, null if insufficient information to create a date 604 * @throws DateTimeException if the date cannot be resolved, typically 605 * because of a conflict in the input data 606 */ 607 @Override // override for performance 608 public LocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 609 return (LocalDate) super.resolveDate(fieldValues, resolverStyle); 610 } 611 612 @Override // override for better proleptic algorithm 613 void resolveProlepticMonth(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 614 Long pMonth = fieldValues.remove(PROLEPTIC_MONTH); 615 if (pMonth != null) { 616 if (resolverStyle != ResolverStyle.LENIENT) { 617 PROLEPTIC_MONTH.checkValidValue(pMonth); 618 } 619 addFieldValue(fieldValues, MONTH_OF_YEAR, Math.floorMod(pMonth, 12) + 1); 620 addFieldValue(fieldValues, YEAR, Math.floorDiv(pMonth, 12)); 621 } 622 } 623 624 @Override // override for enhanced behaviour 625 LocalDate resolveYearOfEra(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 626 Long yoeLong = fieldValues.remove(YEAR_OF_ERA); 627 if (yoeLong != null) { 628 if (resolverStyle != ResolverStyle.LENIENT) { 629 YEAR_OF_ERA.checkValidValue(yoeLong); 630 } 631 Long era = fieldValues.remove(ERA); 632 if (era == null) { 633 Long year = fieldValues.get(YEAR); 634 if (resolverStyle == ResolverStyle.STRICT) { 635 // do not invent era if strict, but do cross-check with year 636 if (year != null) { 637 addFieldValue(fieldValues, YEAR, (year > 0 ? yoeLong: Math.subtractExact(1, yoeLong))); 638 } else { 639 // reinstate the field removed earlier, no cross-check issues 640 fieldValues.put(YEAR_OF_ERA, yoeLong); 641 } 642 } else { 643 // invent era 644 addFieldValue(fieldValues, YEAR, (year == null || year > 0 ? yoeLong: Math.subtractExact(1, yoeLong))); 645 } 646 } else if (era.longValue() == 1L) { 647 addFieldValue(fieldValues, YEAR, yoeLong); 648 } else if (era.longValue() == 0L) { 649 addFieldValue(fieldValues, YEAR, Math.subtractExact(1, yoeLong)); 650 } else { 651 throw new DateTimeException("Invalid value for era: " + era); 652 } 653 } else if (fieldValues.containsKey(ERA)) { 654 ERA.checkValidValue(fieldValues.get(ERA)); // always validated 655 } 656 return null; 657 } 658 659 @Override // override for performance 660 LocalDate resolveYMD(Map <TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 661 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 662 if (resolverStyle == ResolverStyle.LENIENT) { 663 long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1); 664 long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1); 665 return LocalDate.of(y, 1, 1).plusMonths(months).plusDays(days); 666 } 667 int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR)); 668 int dom = DAY_OF_MONTH.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH)); 669 if (resolverStyle == ResolverStyle.SMART) { // previous valid 670 if (moy == 4 || moy == 6 || moy == 9 || moy == 11) { 671 dom = Math.min(dom, 30); 672 } else if (moy == 2) { 673 dom = Math.min(dom, Month.FEBRUARY.length(Year.isLeap(y))); 674 675 } 676 } 677 return LocalDate.of(y, moy, dom); 678 } 679 680 //----------------------------------------------------------------------- 681 @Override 682 public ValueRange range(ChronoField field) { 683 return field.range(); 684 } 685 686 //----------------------------------------------------------------------- 687 /** 688 * Obtains a period for this chronology based on years, months and days. 689 * <p> 690 * This returns a period tied to the ISO chronology using the specified 691 * years, months and days. See {@link Period} for further details. 692 * 693 * @param years the number of years, may be negative 694 * @param months the number of years, may be negative 695 * @param days the number of years, may be negative 696 * @return the period in terms of this chronology, not null 697 * @return the ISO period, not null 698 */ 699 @Override // override with covariant return type 700 public Period period(int years, int months, int days) { 701 return Period.of(years, months, days); 702 } 703 704 //----------------------------------------------------------------------- 705 /** 706 * Writes the Chronology using a 707 * <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>. 708 * @serialData 709 * <pre> 710 * out.writeByte(1); // identifies a Chronology 711 * out.writeUTF(getId()); 712 * </pre> 713 * 714 * @return the instance of {@code Ser}, not null 715 */ 716 @Override 717 Object writeReplace() { 718 return super.writeReplace(); 719 } 720 721 /** 722 * Defend against malicious streams. 723 * 724 * @param s the stream to read 725 * @throws InvalidObjectException always 726 */ 727 private void readObject(ObjectInputStream s) throws InvalidObjectException { 728 throw new InvalidObjectException("Deserialization via serialization delegate"); 729 } 730 }