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

Print this page




  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.temporal;
  63 
  64 import static java.time.temporal.ChronoField.ERA;
  65 import static java.time.temporal.ChronoUnit.ERAS;
  66 
  67 import java.time.DateTimeException;
  68 import java.time.format.DateTimeFormatterBuilder;
  69 import java.time.format.TextStyle;








  70 import java.util.Locale;
  71 
  72 /**
  73  * An era of the time-line.
  74  * <p>
  75  * Most calendar systems have a single epoch dividing the time-line into two eras.
  76  * However, some calendar systems, have multiple eras, such as one for the reign
  77  * of each leader.
  78  * In all cases, the era is conceptually the largest division of the time-line.
  79  * Each chronology defines the Era's that are known Eras and a
  80  * {@link Chrono#eras Chrono.eras} to get the valid eras.
  81  * <p>
  82  * For example, the Thai Buddhist calendar system divides time into two eras,
  83  * before and after a single date. By contrast, the Japanese calendar system
  84  * has one era for the reign of each Emperor.
  85  * <p>
  86  * Instances of {@code Era} may be compared using the {@code ==} operator.
  87  *
  88  * <h3>Specification for implementors</h3>
  89  * This interface must be implemented with care to ensure other classes operate correctly.
  90  * All implementations must be singletons - final, immutable and thread-safe.
  91  * It is recommended to use an enum whenever possible.
  92  *
  93  * @param <C> the chronology of the era
  94  * @since 1.8
  95  */
  96 public interface Era<C extends Chrono<C>> extends TemporalAccessor, TemporalAdjuster {
  97 
  98     /**
  99      * Gets the numeric value associated with the era as defined by the chronology.
 100      * Each chronology defines the predefined Eras and methods to list the Eras
 101      * of the chronology.
 102      * <p>
 103      * All fields, including eras, have an associated numeric value.
 104      * The meaning of the numeric value for era is determined by the chronology
 105      * according to these principles:
 106      * <p><ul>
 107      * <li>The era in use at the epoch 1970-01-01 (ISO) has the value 1.
 108      * <li>Later eras have sequentially higher values.
 109      * <li>Earlier eras have sequentially lower values, which may be negative.
 110      * </ul><p>
 111      *
 112      * @return the numeric era value
 113      */
 114     int getValue();
 115 
 116     /**
 117      * Gets the chronology of this era.
 118      * <p>
 119      * The {@code Chrono} represents the calendar system in use.
 120      * This always returns the standard form of the chronology.
 121      *
 122      * @return the chronology, not null
 123      */
 124     C getChrono();
 125 
 126     //-----------------------------------------------------------------------
 127     /**
 128      * Obtains a date in this era given the year-of-era, month, and day.
 129      * <p>
 130      * This era is combined with the given date fields to form a date.
 131      * The year specified must be the year-of-era.
 132      * Methods to create a date from the proleptic-year are on {@code Chrono}.
 133      * This always uses the standard form of the chronology.
 134      * <p>
 135      * This default implementation invokes the factory method on {@link Chrono}.
 136      *
 137      * @param yearOfEra  the calendar system year-of-era
 138      * @param month  the calendar system month-of-year
 139      * @param day  the calendar system day-of-month
 140      * @return a local date based on this era and the specified year-of-era, month and day
 141      */
 142     public default ChronoLocalDate<C> date(int yearOfEra, int month, int day) {
 143         return getChrono().date(this, yearOfEra, month, day);
 144     }
 145 
 146 
 147     /**
 148      * Obtains a date in this era given year-of-era and day-of-year fields.
 149      * <p>
 150      * This era is combined with the given date fields to form a date.
 151      * The year specified must be the year-of-era.
 152      * Methods to create a date from the proleptic-year are on {@code Chrono}.
 153      * This always uses the standard form of the chronology.
 154      * <p>
 155      * This default implementation invokes the factory method on {@link Chrono}.
 156      *
 157      * @param yearOfEra  the calendar system year-of-era
 158      * @param dayOfYear  the calendar system day-of-year
 159      * @return a local date based on this era and the specified year-of-era and day-of-year
 160      */
 161     public default ChronoLocalDate<C> dateYearDay(int yearOfEra, int dayOfYear) {
 162         return getChrono().dateYearDay(this, yearOfEra, dayOfYear);
 163     }
 164 
 165     //-----------------------------------------------------------------------
 166     /**
 167      * Checks if the specified field is supported.
 168      * <p>
 169      * This checks if this era can be queried for the specified field.
 170      * If false, then calling the {@link #range(TemporalField) range} and
 171      * {@link #get(TemporalField) get} methods will throw an exception.
 172      * <p>
 173      * If the field is a {@link ChronoField} then the query is implemented here.
 174      * The {@code ERA} field returns true.
 175      * All other {@code ChronoField} instances will return false.
 176      * <p>
 177      * If the field is not a {@code ChronoField}, then the result of this method
 178      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
 179      * passing {@code this} as the argument.
 180      * Whether the field is supported is determined by the field.
 181      *
 182      * @param field  the field to check, null returns false
 183      * @return true if the field is supported on this era, false if not
 184      */
 185     @Override
 186     public default boolean isSupported(TemporalField field) {
 187         if (field instanceof ChronoField) {
 188             return field == ERA;
 189         }
 190         return field != null && field.doIsSupported(this);
 191     }
 192 
 193     /**
 194      * Gets the range of valid values for the specified field.
 195      * <p>
 196      * The range object expresses the minimum and maximum valid values for a field.
 197      * This era is used to enhance the accuracy of the returned range.
 198      * If it is not possible to return the range, because the field is not supported
 199      * or for some other reason, an exception is thrown.
 200      * <p>
 201      * If the field is a {@link ChronoField} then the query is implemented here.
 202      * The {@code ERA} field returns the range.
 203      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 204      * <p>
 205      * If the field is not a {@code ChronoField}, then the result of this method
 206      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
 207      * passing {@code this} as the argument.
 208      * Whether the range can be obtained is determined by the field.
 209      *
 210      * @param field  the field to query the range for, not null
 211      * @return the range of valid values for the field, not null
 212      * @throws DateTimeException if the range for the field cannot be obtained
 213      */
 214     @Override  // override for Javadoc
 215     public default ValueRange range(TemporalField field) {
 216         return TemporalAccessor.super.range(field);
 217     }
 218 
 219     /**
 220      * Gets the value of the specified field from this era as an {@code int}.
 221      * <p>
 222      * This queries this era for the value for the specified field.
 223      * The returned value will always be within the valid range of values for the field.
 224      * If it is not possible to return the value, because the field is not supported
 225      * or for some other reason, an exception is thrown.
 226      * <p>
 227      * If the field is a {@link ChronoField} then the query is implemented here.
 228      * The {@code ERA} field returns the value of the era.
 229      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 230      * <p>
 231      * If the field is not a {@code ChronoField}, then the result of this method
 232      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 233      * passing {@code this} as the argument. Whether the value can be obtained,
 234      * and what the value represents, is determined by the field.
 235      *
 236      * @param field  the field to get, not null
 237      * @return the value for the field
 238      * @throws DateTimeException if a value for the field cannot be obtained
 239      * @throws ArithmeticException if numeric overflow occurs
 240      */
 241     @Override  // override for Javadoc and performance
 242     public default int get(TemporalField field) {
 243         if (field == ERA) {
 244             return getValue();
 245         }
 246         return range(field).checkValidIntValue(getLong(field), field);
 247     }
 248 
 249     /**
 250      * Gets the value of the specified field from this era as a {@code long}.
 251      * <p>
 252      * This queries this era for the value for the specified field.
 253      * If it is not possible to return the value, because the field is not supported
 254      * or for some other reason, an exception is thrown.
 255      * <p>
 256      * If the field is a {@link ChronoField} then the query is implemented here.
 257      * The {@code ERA} field returns the value of the era.
 258      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 259      * <p>
 260      * If the field is not a {@code ChronoField}, then the result of this method
 261      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 262      * passing {@code this} as the argument. Whether the value can be obtained,
 263      * and what the value represents, is determined by the field.
 264      *
 265      * @param field  the field to get, not null
 266      * @return the value for the field
 267      * @throws DateTimeException if a value for the field cannot be obtained
 268      * @throws ArithmeticException if numeric overflow occurs
 269      */
 270     @Override
 271     public default long getLong(TemporalField field) {
 272         if (field == ERA) {
 273             return getValue();
 274         } else if (field instanceof ChronoField) {
 275             throw new DateTimeException("Unsupported field: " + field.getName());
 276         }
 277         return field.doGet(this);
 278     }
 279 
 280     //-----------------------------------------------------------------------
 281     /**
 282      * Queries this era using the specified query.
 283      * <p>
 284      * This queries this era using the specified query strategy object.
 285      * The {@code TemporalQuery} object defines the logic to be used to
 286      * obtain the result. Read the documentation of the query to understand
 287      * what the result of this method will be.
 288      * <p>
 289      * The result of this method is obtained by invoking the
 290      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 291      * specified query passing {@code this} as the argument.
 292      *
 293      * @param <R> the type of the result
 294      * @param query  the query to invoke, not null
 295      * @return the query result, null may be returned (defined by the query)
 296      * @throws DateTimeException if unable to query (defined by the query)
 297      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
 298      */
 299     @SuppressWarnings("unchecked")
 300     @Override
 301     public default <R> R query(TemporalQuery<R> query) {
 302         if (query == Queries.chrono()) {
 303             return (R) getChrono();
 304         } else if (query == Queries.precision()) {
 305             return (R) ERAS;
 306         }
 307         return TemporalAccessor.super.query(query);
 308     }
 309 
 310     /**
 311      * Adjusts the specified temporal object to have the same era as this object.
 312      * <p>
 313      * This returns a temporal object of the same observable type as the input
 314      * with the era changed to be the same as this.
 315      * <p>
 316      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
 317      * passing {@link ChronoField#ERA} as the field.
 318      * <p>
 319      * In most cases, it is clearer to reverse the calling pattern by using
 320      * {@link Temporal#with(TemporalAdjuster)}:
 321      * <pre>
 322      *   // these two lines are equivalent, but the second approach is recommended
 323      *   temporal = thisEra.adjustInto(temporal);
 324      *   temporal = temporal.with(thisEra);
 325      * </pre>
 326      * <p>
 327      * This instance is immutable and unaffected by this method call.
 328      *
 329      * @param temporal  the target object to be adjusted, not null
 330      * @return the adjusted object, not null
 331      * @throws DateTimeException if unable to make the adjustment
 332      * @throws ArithmeticException if numeric overflow occurs
 333      */
 334     @Override
 335     public default Temporal adjustInto(Temporal temporal) {
 336         return temporal.with(ERA, getValue());
 337     }
 338 
 339     //-----------------------------------------------------------------------
 340     /**
 341      * Gets the textual representation of this era.
 342      * <p>
 343      * This returns the textual name used to identify the era.

 344      * The parameters control the style of the returned text and the locale.
 345      * <p>
 346      * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
 347      * <p>
 348      * This default implementation is suitable for all implementations.
 349      *
 350      * @param style  the style of the text required, not null
 351      * @param locale  the locale to use, not null
 352      * @return the text value of the era, not null
 353      */
 354     public default String getText(TextStyle style, Locale locale) {
 355         return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).print(this);
 356     }
 357 
 358     // NOTE: methods to convert year-of-era/proleptic-year cannot be here as they may depend on month/day (Japanese)
 359 }


  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.ERA;
  65 import static java.time.temporal.ChronoUnit.ERAS;
  66 
  67 import java.time.DateTimeException;
  68 import java.time.format.DateTimeFormatterBuilder;
  69 import java.time.format.TextStyle;
  70 import java.time.temporal.ChronoField;
  71 import java.time.temporal.Queries;
  72 import java.time.temporal.Temporal;
  73 import java.time.temporal.TemporalAccessor;
  74 import java.time.temporal.TemporalAdjuster;
  75 import java.time.temporal.TemporalField;
  76 import java.time.temporal.TemporalQuery;
  77 import java.time.temporal.ValueRange;
  78 import java.util.Locale;
  79 
  80 /**
  81  * An era of the time-line.
  82  * <p>
  83  * Most calendar systems have a single epoch dividing the time-line into two eras.
  84  * However, some calendar systems, have multiple eras, such as one for the reign
  85  * of each leader.
  86  * In all cases, the era is conceptually the largest division of the time-line.
  87  * Each chronology defines the Era's that are known Eras and a
  88  * {@link Chronology#eras Chronology.eras} to get the valid eras.
  89  * <p>
  90  * For example, the Thai Buddhist calendar system divides time into two eras,
  91  * before and after a single date. By contrast, the Japanese calendar system
  92  * has one era for the reign of each Emperor.
  93  * <p>
  94  * Instances of {@code Era} may be compared using the {@code ==} operator.
  95  *
  96  * <h3>Specification for implementors</h3>
  97  * This interface must be implemented with care to ensure other classes operate correctly.
  98  * All implementations must be singletons - final, immutable and thread-safe.
  99  * It is recommended to use an enum whenever possible.
 100  *

 101  * @since 1.8
 102  */
 103 public interface Era extends TemporalAccessor, TemporalAdjuster {
 104 
 105     /**
 106      * Gets the numeric value associated with the era as defined by the chronology.
 107      * Each chronology defines the predefined Eras and methods to list the Eras
 108      * of the chronology.
 109      * <p>
 110      * All fields, including eras, have an associated numeric value.
 111      * The meaning of the numeric value for era is determined by the chronology
 112      * according to these principles:
 113      * <p><ul>
 114      * <li>The era in use at the epoch 1970-01-01 (ISO) has the value 1.
 115      * <li>Later eras have sequentially higher values.
 116      * <li>Earlier eras have sequentially lower values, which may be negative.
 117      * </ul><p>
 118      *
 119      * @return the numeric era value
 120      */
 121     int getValue();
 122 
 123     /**
 124      * Gets the chronology of this era.
 125      * <p>
 126      * The {@code Chronology} represents the calendar system in use.
 127      * This always returns the standard form of the chronology.
 128      *
 129      * @return the chronology, not null
 130      */
 131     Chronology getChronology();
 132 
 133     //-----------------------------------------------------------------------
 134     /**
 135      * Obtains a date in this era given the year-of-era, month, and day.
 136      * <p>
 137      * This era is combined with the given date fields to form a date.
 138      * The year specified must be the year-of-era.
 139      * Methods to create a date from the proleptic-year are on {@code Chronology}.
 140      * This always uses the standard form of the chronology.
 141      * <p>
 142      * This default implementation invokes the factory method on {@link Chronology}.
 143      *
 144      * @param yearOfEra  the calendar system year-of-era
 145      * @param month  the calendar system month-of-year
 146      * @param day  the calendar system day-of-month
 147      * @return a local date based on this era and the specified year-of-era, month and day
 148      */
 149     public default ChronoLocalDate date(int yearOfEra, int month, int day) {
 150         return getChronology().date(this, yearOfEra, month, day);
 151     }
 152 
 153 
 154     /**
 155      * Obtains a date in this era given year-of-era and day-of-year fields.
 156      * <p>
 157      * This era is combined with the given date fields to form a date.
 158      * The year specified must be the year-of-era.
 159      * Methods to create a date from the proleptic-year are on {@code Chronology}.
 160      * This always uses the standard form of the chronology.
 161      * <p>
 162      * This default implementation invokes the factory method on {@link Chronology}.
 163      *
 164      * @param yearOfEra  the calendar system year-of-era
 165      * @param dayOfYear  the calendar system day-of-year
 166      * @return a local date based on this era and the specified year-of-era and day-of-year
 167      */
 168     public default ChronoLocalDate dateYearDay(int yearOfEra, int dayOfYear) {
 169         return getChronology().dateYearDay(this, yearOfEra, dayOfYear);
 170     }
 171 
 172     //-----------------------------------------------------------------------
 173     /**
 174      * Checks if the specified field is supported.
 175      * <p>
 176      * This checks if this era can be queried for the specified field.
 177      * If false, then calling the {@link #range(TemporalField) range} and
 178      * {@link #get(TemporalField) get} methods will throw an exception.
 179      * <p>
 180      * If the field is a {@link ChronoField} then the query is implemented here.
 181      * The {@code ERA} field returns true.
 182      * All other {@code ChronoField} instances will return false.
 183      * <p>
 184      * If the field is not a {@code ChronoField}, then the result of this method
 185      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 186      * passing {@code this} as the argument.
 187      * Whether the field is supported is determined by the field.
 188      *
 189      * @param field  the field to check, null returns false
 190      * @return true if the field is supported on this era, false if not
 191      */
 192     @Override
 193     public default boolean isSupported(TemporalField field) {
 194         if (field instanceof ChronoField) {
 195             return field == ERA;
 196         }
 197         return field != null && field.isSupportedBy(this);
 198     }
 199 
 200     /**
 201      * Gets the range of valid values for the specified field.
 202      * <p>
 203      * The range object expresses the minimum and maximum valid values for a field.
 204      * This era is used to enhance the accuracy of the returned range.
 205      * If it is not possible to return the range, because the field is not supported
 206      * or for some other reason, an exception is thrown.
 207      * <p>
 208      * If the field is a {@link ChronoField} then the query is implemented here.
 209      * The {@code ERA} field returns the range.
 210      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 211      * <p>
 212      * If the field is not a {@code ChronoField}, then the result of this method
 213      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 214      * passing {@code this} as the argument.
 215      * Whether the range can be obtained is determined by the field.
 216      *
 217      * @param field  the field to query the range for, not null
 218      * @return the range of valid values for the field, not null
 219      * @throws DateTimeException if the range for the field cannot be obtained
 220      */
 221     @Override  // override for Javadoc
 222     public default ValueRange range(TemporalField field) {
 223         return TemporalAccessor.super.range(field);
 224     }
 225 
 226     /**
 227      * Gets the value of the specified field from this era as an {@code int}.
 228      * <p>
 229      * This queries this era for the value for the specified field.
 230      * The returned value will always be within the valid range of values for the field.
 231      * If it is not possible to return the value, because the field is not supported
 232      * or for some other reason, an exception is thrown.
 233      * <p>
 234      * If the field is a {@link ChronoField} then the query is implemented here.
 235      * The {@code ERA} field returns the value of the era.
 236      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 237      * <p>
 238      * If the field is not a {@code ChronoField}, then the result of this method
 239      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 240      * passing {@code this} as the argument. Whether the value can be obtained,
 241      * and what the value represents, is determined by the field.
 242      *
 243      * @param field  the field to get, not null
 244      * @return the value for the field
 245      * @throws DateTimeException if a value for the field cannot be obtained
 246      * @throws ArithmeticException if numeric overflow occurs
 247      */
 248     @Override  // override for Javadoc and performance
 249     public default int get(TemporalField field) {
 250         if (field == ERA) {
 251             return getValue();
 252         }
 253         return range(field).checkValidIntValue(getLong(field), field);
 254     }
 255 
 256     /**
 257      * Gets the value of the specified field from this era as a {@code long}.
 258      * <p>
 259      * This queries this era for the value for the specified field.
 260      * If it is not possible to return the value, because the field is not supported
 261      * or for some other reason, an exception is thrown.
 262      * <p>
 263      * If the field is a {@link ChronoField} then the query is implemented here.
 264      * The {@code ERA} field returns the value of the era.
 265      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 266      * <p>
 267      * If the field is not a {@code ChronoField}, then the result of this method
 268      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 269      * passing {@code this} as the argument. Whether the value can be obtained,
 270      * and what the value represents, is determined by the field.
 271      *
 272      * @param field  the field to get, not null
 273      * @return the value for the field
 274      * @throws DateTimeException if a value for the field cannot be obtained
 275      * @throws ArithmeticException if numeric overflow occurs
 276      */
 277     @Override
 278     public default long getLong(TemporalField field) {
 279         if (field == ERA) {
 280             return getValue();
 281         } else if (field instanceof ChronoField) {
 282             throw new DateTimeException("Unsupported field: " + field.getName());
 283         }
 284         return field.getFrom(this);
 285     }
 286 
 287     //-----------------------------------------------------------------------
 288     /**
 289      * Queries this era using the specified query.
 290      * <p>
 291      * This queries this era using the specified query strategy object.
 292      * The {@code TemporalQuery} object defines the logic to be used to
 293      * obtain the result. Read the documentation of the query to understand
 294      * what the result of this method will be.
 295      * <p>
 296      * The result of this method is obtained by invoking the
 297      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 298      * specified query passing {@code this} as the argument.
 299      *
 300      * @param <R> the type of the result
 301      * @param query  the query to invoke, not null
 302      * @return the query result, null may be returned (defined by the query)
 303      * @throws DateTimeException if unable to query (defined by the query)
 304      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
 305      */
 306     @SuppressWarnings("unchecked")
 307     @Override
 308     public default <R> R query(TemporalQuery<R> query) {
 309         if (query == Queries.chronology()) {
 310             return (R) getChronology();
 311         } else if (query == Queries.precision()) {
 312             return (R) ERAS;
 313         }
 314         return TemporalAccessor.super.query(query);
 315     }
 316 
 317     /**
 318      * Adjusts the specified temporal object to have the same era as this object.
 319      * <p>
 320      * This returns a temporal object of the same observable type as the input
 321      * with the era changed to be the same as this.
 322      * <p>
 323      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
 324      * passing {@link ChronoField#ERA} as the field.
 325      * <p>
 326      * In most cases, it is clearer to reverse the calling pattern by using
 327      * {@link Temporal#with(TemporalAdjuster)}:
 328      * <pre>
 329      *   // these two lines are equivalent, but the second approach is recommended
 330      *   temporal = thisEra.adjustInto(temporal);
 331      *   temporal = temporal.with(thisEra);
 332      * </pre>
 333      * <p>
 334      * This instance is immutable and unaffected by this method call.
 335      *
 336      * @param temporal  the target object to be adjusted, not null
 337      * @return the adjusted object, not null
 338      * @throws DateTimeException if unable to make the adjustment
 339      * @throws ArithmeticException if numeric overflow occurs
 340      */
 341     @Override
 342     public default Temporal adjustInto(Temporal temporal) {
 343         return temporal.with(ERA, getValue());
 344     }
 345 
 346     //-----------------------------------------------------------------------
 347     /**
 348      * Gets the textual representation of this era.
 349      * <p>
 350      * This returns the textual name used to identify the era,
 351      * suitable for presentation to the user.
 352      * The parameters control the style of the returned text and the locale.
 353      * <p>
 354      * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
 355      * <p>
 356      * This default implementation is suitable for all implementations.
 357      *
 358      * @param style  the style of the text required, not null
 359      * @param locale  the locale to use, not null
 360      * @return the text value of the era, not null
 361      */
 362     public default String getDisplayName(TextStyle style, Locale locale) {
 363         return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).format(this);
 364     }
 365 
 366     // NOTE: methods to convert year-of-era/proleptic-year cannot be here as they may depend on month/day (Japanese)
 367 }