1 /* 2 * Copyright (c) 2012, 2013, 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.ALIGNED_DAY_OF_WEEK_IN_MONTH; 65 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 66 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 67 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 68 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 69 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 70 import static java.time.temporal.ChronoField.DAY_OF_YEAR; 71 import static java.time.temporal.ChronoField.EPOCH_DAY; 72 import static java.time.temporal.ChronoField.ERA; 73 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 74 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH; 75 import static java.time.temporal.ChronoField.YEAR; 76 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 77 import static java.time.temporal.TemporalAdjuster.nextOrSame; 78 79 import java.io.Serializable; 80 import java.time.Clock; 81 import java.time.DateTimeException; 82 import java.time.DayOfWeek; 83 import java.time.Instant; 84 import java.time.LocalDate; 85 import java.time.LocalDateTime; 86 import java.time.Month; 87 import java.time.Year; 88 import java.time.ZoneId; 89 import java.time.ZonedDateTime; 90 import java.time.format.ResolverStyle; 91 import java.time.temporal.ChronoField; 92 import java.time.temporal.TemporalAccessor; 93 import java.time.temporal.TemporalField; 94 import java.time.temporal.ValueRange; 95 import java.util.Arrays; 96 import java.util.List; 97 import java.util.Locale; 98 import java.util.Map; 99 import java.util.Objects; 100 101 /** 102 * The ISO calendar system. 103 * <p> 104 * This chronology defines the rules of the ISO calendar system. 105 * This calendar system is based on the ISO-8601 standard, which is the 106 * <i>de facto</i> world calendar. 107 * <p> 108 * The fields are defined as follows: 109 * <p><ul> 110 * <li>era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE). 111 * <li>year-of-era - The year-of-era is the same as the proleptic-year for the current CE era. 112 * For the BCE era before the ISO epoch the year increases from 1 upwards as time goes backwards. 113 * <li>proleptic-year - The proleptic year is the same as the year-of-era for the 114 * current era. For the previous era, years have zero, then negative values. 115 * <li>month-of-year - There are 12 months in an ISO year, numbered from 1 to 12. 116 * <li>day-of-month - There are between 28 and 31 days in each of the ISO month, numbered from 1 to 31. 117 * Months 4, 6, 9 and 11 have 30 days, Months 1, 3, 5, 7, 8, 10 and 12 have 31 days. 118 * Month 2 has 28 days, or 29 in a leap year. 119 * <li>day-of-year - There are 365 days in a standard ISO year and 366 in a leap year. 120 * The days are numbered from 1 to 365 or 1 to 366. 121 * <li>leap-year - Leap years occur every 4 years, except where the year is divisble by 100 and not divisble by 400. 122 * </ul><p> 123 * 124 * <h3>Specification for implementors</h3> 125 * This class is immutable and thread-safe. 126 * 127 * @since 1.8 128 */ 129 public final class IsoChronology extends Chronology implements Serializable { 130 131 /** 132 * Singleton instance of the ISO chronology. 133 */ 134 public static final IsoChronology INSTANCE = new IsoChronology(); 135 136 /** 137 * Serialization version. 138 */ 139 private static final long serialVersionUID = -1440403870442975015L; 140 141 /** 142 * Restricted constructor. 143 */ 144 private IsoChronology() { 145 } 146 147 //----------------------------------------------------------------------- 148 /** 149 * Gets the ID of the chronology - 'ISO'. 150 * <p> 151 * The ID uniquely identifies the {@code Chronology}. 152 * It can be used to lookup the {@code Chronology} using {@link #of(String)}. 153 * 154 * @return the chronology ID - 'ISO' 155 * @see #getCalendarType() 156 */ 157 @Override 158 public String getId() { 159 return "ISO"; 160 } 161 162 /** 163 * Gets the calendar type of the underlying calendar system - 'iso8601'. 164 * <p> 165 * The calendar type is an identifier defined by the 166 * <em>Unicode Locale Data Markup Language (LDML)</em> specification. 167 * It can be used to lookup the {@code Chronology} using {@link #of(String)}. 168 * It can also be used as part of a locale, accessible via 169 * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'. 170 * 171 * @return the calendar system type - 'iso8601' 172 * @see #getId() 173 */ 174 @Override 175 public String getCalendarType() { 176 return "iso8601"; 177 } 178 179 //----------------------------------------------------------------------- 180 /** 181 * Obtains an ISO local date from the era, year-of-era, month-of-year 182 * and day-of-month fields. 183 * 184 * @param era the ISO era, not null 185 * @param yearOfEra the ISO year-of-era 186 * @param month the ISO month-of-year 187 * @param dayOfMonth the ISO day-of-month 188 * @return the ISO local date, not null 189 * @throws DateTimeException if unable to create the date 190 * @throws ClassCastException if the type of {@code era} is not {@code IsoEra} 191 */ 192 @Override // override with covariant return type 193 public LocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) { 194 return date(prolepticYear(era, yearOfEra), month, dayOfMonth); 195 } 196 197 /** 198 * Obtains an ISO local date from the proleptic-year, month-of-year 199 * and day-of-month fields. 200 * <p> 201 * This is equivalent to {@link LocalDate#of(int, int, int)}. 202 * 203 * @param prolepticYear the ISO proleptic-year 204 * @param month the ISO month-of-year 205 * @param dayOfMonth the ISO day-of-month 206 * @return the ISO local date, not null 207 * @throws DateTimeException if unable to create the date 208 */ 209 @Override // override with covariant return type 210 public LocalDate date(int prolepticYear, int month, int dayOfMonth) { 211 return LocalDate.of(prolepticYear, month, dayOfMonth); 212 } 213 214 /** 215 * Obtains an ISO local date from the era, year-of-era and day-of-year fields. 216 * 217 * @param era the ISO era, not null 218 * @param yearOfEra the ISO year-of-era 219 * @param dayOfYear the ISO day-of-year 220 * @return the ISO local date, not null 221 * @throws DateTimeException if unable to create the date 222 */ 223 @Override // override with covariant return type 224 public LocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) { 225 return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear); 226 } 227 228 /** 229 * Obtains an ISO local date from the proleptic-year and day-of-year fields. 230 * <p> 231 * This is equivalent to {@link LocalDate#ofYearDay(int, int)}. 232 * 233 * @param prolepticYear the ISO proleptic-year 234 * @param dayOfYear the ISO day-of-year 235 * @return the ISO local date, not null 236 * @throws DateTimeException if unable to create the date 237 */ 238 @Override // override with covariant return type 239 public LocalDate dateYearDay(int prolepticYear, int dayOfYear) { 240 return LocalDate.ofYearDay(prolepticYear, dayOfYear); 241 } 242 243 /** 244 * Obtains an ISO local date from the epoch-day. 245 * <p> 246 * This is equivalent to {@link LocalDate#ofEpochDay(long)}. 247 * 248 * @param epochDay the epoch day 249 * @return the ISO local date, not null 250 * @throws DateTimeException if unable to create the date 251 */ 252 @Override // override with covariant return type 253 public LocalDate dateEpochDay(long epochDay) { 254 return LocalDate.ofEpochDay(epochDay); 255 } 256 257 //----------------------------------------------------------------------- 258 /** 259 * Obtains an ISO local date from another date-time object. 260 * <p> 261 * This is equivalent to {@link LocalDate#from(TemporalAccessor)}. 262 * 263 * @param temporal the date-time object to convert, not null 264 * @return the ISO local date, not null 265 * @throws DateTimeException if unable to create the date 266 */ 267 @Override // override with covariant return type 268 public LocalDate date(TemporalAccessor temporal) { 269 return LocalDate.from(temporal); 270 } 271 272 /** 273 * Obtains an ISO local date-time from another date-time object. 274 * <p> 275 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}. 276 * 277 * @param temporal the date-time object to convert, not null 278 * @return the ISO local date-time, not null 279 * @throws DateTimeException if unable to create the date-time 280 */ 281 @Override // override with covariant return type 282 public LocalDateTime localDateTime(TemporalAccessor temporal) { 283 return LocalDateTime.from(temporal); 284 } 285 286 /** 287 * Obtains an ISO zoned date-time from another date-time object. 288 * <p> 289 * This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}. 290 * 291 * @param temporal the date-time object to convert, not null 292 * @return the ISO zoned date-time, not null 293 * @throws DateTimeException if unable to create the date-time 294 */ 295 @Override // override with covariant return type 296 public ZonedDateTime zonedDateTime(TemporalAccessor temporal) { 297 return ZonedDateTime.from(temporal); 298 } 299 300 /** 301 * Obtains an ISO zoned date-time in this chronology from an {@code Instant}. 302 * <p> 303 * This is equivalent to {@link ZonedDateTime#ofInstant(Instant, ZoneId)}. 304 * 305 * @param instant the instant to create the date-time from, not null 306 * @param zone the time-zone, not null 307 * @return the zoned date-time, not null 308 * @throws DateTimeException if the result exceeds the supported range 309 */ 310 @Override 311 public ZonedDateTime zonedDateTime(Instant instant, ZoneId zone) { 312 return ZonedDateTime.ofInstant(instant, zone); 313 } 314 315 //----------------------------------------------------------------------- 316 /** 317 * Obtains the current ISO local date from the system clock in the default time-zone. 318 * <p> 319 * This will query the {@link Clock#systemDefaultZone() system clock} in the default 320 * time-zone to obtain the current date. 321 * <p> 322 * Using this method will prevent the ability to use an alternate clock for testing 323 * because the clock is hard-coded. 324 * 325 * @return the current ISO local date using the system clock and default time-zone, not null 326 * @throws DateTimeException if unable to create the date 327 */ 328 @Override // override with covariant return type 329 public LocalDate dateNow() { 330 return dateNow(Clock.systemDefaultZone()); 331 } 332 333 /** 334 * Obtains the current ISO local date from the system clock in the specified time-zone. 335 * <p> 336 * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. 337 * Specifying the time-zone avoids dependence on the default time-zone. 338 * <p> 339 * Using this method will prevent the ability to use an alternate clock for testing 340 * because the clock is hard-coded. 341 * 342 * @return the current ISO local date using the system clock, not null 343 * @throws DateTimeException if unable to create the date 344 */ 345 @Override // override with covariant return type 346 public LocalDate dateNow(ZoneId zone) { 347 return dateNow(Clock.system(zone)); 348 } 349 350 /** 351 * Obtains the current ISO local date from the specified clock. 352 * <p> 353 * This will query the specified clock to obtain the current date - today. 354 * Using this method allows the use of an alternate clock for testing. 355 * The alternate clock may be introduced using {@link Clock dependency injection}. 356 * 357 * @param clock the clock to use, not null 358 * @return the current ISO local date, not null 359 * @throws DateTimeException if unable to create the date 360 */ 361 @Override // override with covariant return type 362 public LocalDate dateNow(Clock clock) { 363 Objects.requireNonNull(clock, "clock"); 364 return date(LocalDate.now(clock)); 365 } 366 367 //----------------------------------------------------------------------- 368 /** 369 * Checks if the year is a leap year, according to the ISO proleptic 370 * calendar system rules. 371 * <p> 372 * This method applies the current rules for leap years across the whole time-line. 373 * In general, a year is a leap year if it is divisible by four without 374 * remainder. However, years divisible by 100, are not leap years, with 375 * the exception of years divisible by 400 which are. 376 * <p> 377 * For example, 1904 is a leap year it is divisible by 4. 378 * 1900 was not a leap year as it is divisible by 100, however 2000 was a 379 * leap year as it is divisible by 400. 380 * <p> 381 * The calculation is proleptic - applying the same rules into the far future and far past. 382 * This is historically inaccurate, but is correct for the ISO-8601 standard. 383 * 384 * @param prolepticYear the ISO proleptic year to check 385 * @return true if the year is leap, false otherwise 386 */ 387 @Override 388 public boolean isLeapYear(long prolepticYear) { 389 return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0); 390 } 391 392 @Override 393 public int prolepticYear(Era era, int yearOfEra) { 394 if (era instanceof IsoEra == false) { 395 throw new ClassCastException("Era must be IsoEra"); 396 } 397 return (era == IsoEra.CE ? yearOfEra : 1 - yearOfEra); 398 } 399 400 @Override 401 public Era eraOf(int eraValue) { 402 return IsoEra.of(eraValue); 403 } 404 405 @Override 406 public List<Era> eras() { 407 return Arrays.<Era>asList(IsoEra.values()); 408 } 409 410 //----------------------------------------------------------------------- 411 /** 412 * Resolves parsed {@code ChronoField} values into a date during parsing. 413 * <p> 414 * Most {@code TemporalField} implementations are resolved using the 415 * resolve method on the field. By contrast, the {@code ChronoField} class 416 * defines fields that only have meaning relative to the chronology. 417 * As such, {@code ChronoField} date fields are resolved here in the 418 * context of a specific chronology. 419 * <p> 420 * {@code ChronoField} instances on the ISO calendar system are resolved 421 * as follows. 422 * <ul> 423 * <li>{@code EPOCH_DAY} - If present, this is converted to a {@code LocalDate} 424 * all other date fields are then cross-checked against the date 425 * <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the 426 * {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart 427 * then the field is validated. 428 * <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they 429 * are combined to form a {@code YEAR}. In lenient mode, the {@code YEAR_OF_ERA} 430 * range is not validated, in smart and strict mode it is. The {@code ERA} is 431 * validated for range in all three modes. If only the {@code YEAR_OF_ERA} is 432 * present, and the mode is smart or lenient, then the current era (CE/AD) 433 * is assumed. In strict mode, no ers is assumed and the {@code YEAR_OF_ERA} is 434 * left untouched. If only the {@code ERA} is present, then it is left untouched. 435 * <li>{@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} - 436 * If all three are present, then they are combined to form a {@code LocalDate}. 437 * In all three modes, the {@code YEAR} is validated. If the mode is smart or strict, 438 * then the month and day are validated, with the day validated from 1 to 31. 439 * If the mode is lenient, then the date is combined in a manner equivalent to 440 * creating a date on the first of January in the requested year, then adding 441 * the difference in months, then the difference in days. 442 * If the mode is smart, and the day-of-month is greater than the maximum for 443 * the year-month, then the day-of-month is adjusted to the last day-of-month. 444 * If the mode is strict, then the three fields must form a valid date. 445 * <li>{@code YEAR} and {@code DAY_OF_YEAR} - 446 * If both are present, then they are combined to form a {@code LocalDate}. 447 * In all three modes, the {@code YEAR} is validated. 448 * If the mode is lenient, then the date is combined in a manner equivalent to 449 * creating a date on the first of January in the requested year, then adding 450 * the difference in days. 451 * If the mode is smart or strict, then the two fields must form a valid date. 452 * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and 453 * {@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - 454 * If all four are present, then they are combined to form a {@code LocalDate}. 455 * In all three modes, the {@code YEAR} is validated. 456 * If the mode is lenient, then the date is combined in a manner equivalent to 457 * creating a date on the first of January in the requested year, then adding 458 * the difference in months, then the difference in weeks, then in days. 459 * If the mode is smart or strict, then the all four fields are validated to 460 * their outer ranges. The date is then combined in a manner equivalent to 461 * creating a date on the first day of the requested year and month, then adding 462 * the amount in weeks and days to reach their values. If the mode is strict, 463 * the date is additionally validated to check that the day and week adjustment 464 * did not change the month. 465 * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and 466 * {@code DAY_OF_WEEK} - If all four are present, then they are combined to 467 * form a {@code LocalDate}. The approach is the same as described above for 468 * years, months and weeks in {@code ALIGNED_DAY_OF_WEEK_IN_MONTH}. 469 * The day-of-week is adjusted as the next or same matching day-of-week once 470 * the years, months and weeks have been handled. 471 * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - 472 * If all three are present, then they are combined to form a {@code LocalDate}. 473 * In all three modes, the {@code YEAR} is validated. 474 * If the mode is lenient, then the date is combined in a manner equivalent to 475 * creating a date on the first of January in the requested year, then adding 476 * the difference in weeks, then in days. 477 * If the mode is smart or strict, then the all three fields are validated to 478 * their outer ranges. The date is then combined in a manner equivalent to 479 * creating a date on the first day of the requested year, then adding 480 * the amount in weeks and days to reach their values. If the mode is strict, 481 * the date is additionally validated to check that the day and week adjustment 482 * did not change the year. 483 * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code DAY_OF_WEEK} - 484 * If all three are present, then they are combined to form a {@code LocalDate}. 485 * The approach is the same as described above for years and weeks in 486 * {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the 487 * next or same matching day-of-week once the years and weeks have been handled. 488 * </ul> 489 * 490 * @param fieldValues the map of fields to values, which can be updated, not null 491 * @param resolverStyle the requested type of resolve, not null 492 * @return the resolved date, null if insufficient information to create a date 493 * @throws DateTimeException if the date cannot be resolved, typically 494 * because of a conflict in the input data 495 */ 496 @Override // override for performance 497 public LocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 498 // check epoch-day before inventing era 499 if (fieldValues.containsKey(EPOCH_DAY)) { 500 return LocalDate.ofEpochDay(fieldValues.remove(EPOCH_DAY)); 501 } 502 503 // fix proleptic month before inventing era 504 resolveProlepticMonth(fieldValues, resolverStyle); 505 506 // invent era if necessary to resolve year-of-era 507 resolveYearOfEra(fieldValues, resolverStyle); 508 509 // build date 510 if (fieldValues.containsKey(YEAR)) { 511 if (fieldValues.containsKey(MONTH_OF_YEAR)) { 512 if (fieldValues.containsKey(DAY_OF_MONTH)) { 513 return resolveYMD(fieldValues, resolverStyle); 514 } 515 if (fieldValues.containsKey(ALIGNED_WEEK_OF_MONTH)) { 516 if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_MONTH)) { 517 return resolveYMAA(fieldValues, resolverStyle); 518 } 519 if (fieldValues.containsKey(DAY_OF_WEEK)) { 520 return resolveYMAD(fieldValues, resolverStyle); 521 } 522 } 523 } 524 if (fieldValues.containsKey(DAY_OF_YEAR)) { 525 return resolveYD(fieldValues, resolverStyle); 526 } 527 if (fieldValues.containsKey(ALIGNED_WEEK_OF_YEAR)) { 528 if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_YEAR)) { 529 return resolveYAA(fieldValues, resolverStyle); 530 } 531 if (fieldValues.containsKey(DAY_OF_WEEK)) { 532 return resolveYAD(fieldValues, resolverStyle); 533 } 534 } 535 } 536 return null; 537 } 538 539 private void resolveProlepticMonth(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 540 Long pMonth = fieldValues.remove(PROLEPTIC_MONTH); 541 if (pMonth != null) { 542 if (resolverStyle != ResolverStyle.LENIENT) { 543 PROLEPTIC_MONTH.checkValidValue(pMonth); 544 } 545 addFieldValue(fieldValues, MONTH_OF_YEAR, Math.floorMod(pMonth, 12) + 1); 546 addFieldValue(fieldValues, YEAR, Math.floorDiv(pMonth, 12)); 547 } 548 } 549 550 private void resolveYearOfEra(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 551 Long yoeLong = fieldValues.remove(YEAR_OF_ERA); 552 if (yoeLong != null) { 553 if (resolverStyle != ResolverStyle.LENIENT) { 554 YEAR_OF_ERA.checkValidValue(yoeLong); 555 } 556 Long era = fieldValues.remove(ERA); 557 if (era == null) { 558 Long year = fieldValues.get(YEAR); 559 if (resolverStyle == ResolverStyle.STRICT) { 560 // do not invent era if strict, but do cross-check with year 561 if (year != null) { 562 addFieldValue(fieldValues, YEAR, (year > 0 ? yoeLong: Math.subtractExact(1, yoeLong))); 563 } else { 564 // reinstate the field removed earlier, no cross-check issues 565 fieldValues.put(YEAR_OF_ERA, yoeLong); 566 } 567 } else { 568 // invent era 569 addFieldValue(fieldValues, YEAR, (year == null || year > 0 ? yoeLong: Math.subtractExact(1, yoeLong))); 570 } 571 } else if (era.longValue() == 1L) { 572 addFieldValue(fieldValues, YEAR, yoeLong); 573 } else if (era.longValue() == 0L) { 574 addFieldValue(fieldValues, YEAR, Math.subtractExact(1, yoeLong)); 575 } else { 576 throw new DateTimeException("Invalid value for era: " + era); 577 } 578 } 579 } 580 581 private LocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 582 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 583 if (resolverStyle == ResolverStyle.LENIENT) { 584 long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1); 585 long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1); 586 return LocalDate.of(y, 1, 1).plusMonths(months).plusDays(days); 587 } 588 int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR)); 589 int dom = DAY_OF_MONTH.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH)); 590 if (resolverStyle == ResolverStyle.SMART) { // previous valid 591 dom = Math.min(dom, Month.of(moy).length(Year.isLeap(y))); 592 } 593 return LocalDate.of(y, moy, dom); 594 } 595 596 private LocalDate resolveYD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 597 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 598 if (resolverStyle == ResolverStyle.LENIENT) { 599 long days = Math.subtractExact(fieldValues.remove(DAY_OF_YEAR), 1); 600 return LocalDate.of(y, 1, 1).plusDays(days); 601 } 602 int doy = DAY_OF_YEAR.checkValidIntValue(fieldValues.remove(DAY_OF_YEAR)); 603 return LocalDate.ofYearDay(y, doy); // smart is same as strict 604 } 605 606 private LocalDate resolveYMAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 607 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 608 if (resolverStyle == ResolverStyle.LENIENT) { 609 long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1); 610 long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1); 611 long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), 1); 612 return LocalDate.of(y, 1, 1).plusMonths(months).plusWeeks(weeks).plusDays(days); 613 } 614 int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR)); 615 int aw = ALIGNED_WEEK_OF_MONTH.checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH)); 616 int ad = ALIGNED_DAY_OF_WEEK_IN_MONTH.checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH)); 617 LocalDate date = LocalDate.of(y, moy, 1).plusDays((aw - 1) * 7 + (ad - 1)); 618 if (resolverStyle == ResolverStyle.STRICT && date.getMonthValue() != moy) { 619 throw new DateTimeException("Strict mode rejected resolved date as it is in a different month"); 620 } 621 return date; 622 } 623 624 private LocalDate resolveYMAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 625 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 626 if (resolverStyle == ResolverStyle.LENIENT) { 627 long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1); 628 long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1); 629 long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1); 630 return resolveAligned(y, months, weeks, dow); 631 } 632 int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR)); 633 int aw = ALIGNED_WEEK_OF_MONTH.checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH)); 634 int dow = DAY_OF_WEEK.checkValidIntValue(fieldValues.remove(DAY_OF_WEEK)); 635 LocalDate date = LocalDate.of(y, moy, 1).plusDays((aw - 1) * 7).with(nextOrSame(DayOfWeek.of(dow))); 636 if (resolverStyle == ResolverStyle.STRICT && date.getMonthValue() != moy) { 637 throw new DateTimeException("Strict mode rejected resolved date as it is in a different month"); 638 } 639 return date; 640 } 641 642 private LocalDate resolveYAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 643 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 644 if (resolverStyle == ResolverStyle.LENIENT) { 645 long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1); 646 long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), 1); 647 return LocalDate.of(y, 1, 1).plusWeeks(weeks).plusDays(days); 648 } 649 int aw = ALIGNED_WEEK_OF_YEAR.checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR)); 650 int ad = ALIGNED_DAY_OF_WEEK_IN_YEAR.checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR)); 651 LocalDate date = LocalDate.of(y, 1, 1).plusDays((aw - 1) * 7 + (ad - 1)); 652 if (resolverStyle == ResolverStyle.STRICT && date.getYear() != y) { 653 throw new DateTimeException("Strict mode rejected resolved date as it is in a different year"); 654 } 655 return date; 656 } 657 658 private LocalDate resolveYAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 659 int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR)); 660 if (resolverStyle == ResolverStyle.LENIENT) { 661 long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1); 662 long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1); 663 return resolveAligned(y, 0, weeks, dow); 664 } 665 int aw = ALIGNED_WEEK_OF_YEAR.checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR)); 666 int dow = DAY_OF_WEEK.checkValidIntValue(fieldValues.remove(DAY_OF_WEEK)); 667 LocalDate date = LocalDate.of(y, 1, 1).plusDays((aw - 1) * 7).with(nextOrSame(DayOfWeek.of(dow))); 668 if (resolverStyle == ResolverStyle.STRICT && date.getYear() != y) { 669 throw new DateTimeException("Strict mode rejected resolved date as it is in a different year"); 670 } 671 return date; 672 } 673 674 private LocalDate resolveAligned(int y, long months, long weeks, long dow) { 675 LocalDate date = LocalDate.of(y, 1, 1).plusMonths(months).plusWeeks(weeks); 676 if (dow > 7) { 677 date = date.plusWeeks((dow - 1) / 7); 678 dow = ((dow - 1) % 7) + 1; 679 } else if (dow < 1) { 680 date = date.plusWeeks(Math.subtractExact(dow, 7) / 7); 681 dow = ((dow + 6) % 7) + 1; 682 } 683 return date.with(nextOrSame(DayOfWeek.of((int) dow))); 684 } 685 686 //----------------------------------------------------------------------- 687 @Override 688 public ValueRange range(ChronoField field) { 689 return field.range(); 690 } 691 692 }