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