src/share/classes/java/time/Month.java

Print this page




  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;
  63 
  64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  65 import static java.time.temporal.ChronoUnit.MONTHS;
  66 


  67 import java.time.format.DateTimeFormatterBuilder;
  68 import java.time.format.TextStyle;
  69 import java.time.temporal.Chrono;
  70 import java.time.temporal.ChronoField;
  71 import java.time.temporal.ISOChrono;
  72 import java.time.temporal.Queries;
  73 import java.time.temporal.Temporal;
  74 import java.time.temporal.TemporalAccessor;
  75 import java.time.temporal.TemporalAdjuster;
  76 import java.time.temporal.TemporalField;
  77 import java.time.temporal.TemporalQuery;
  78 import java.time.temporal.ValueRange;
  79 import java.util.Locale;
  80 
  81 /**
  82  * A month-of-year, such as 'July'.
  83  * <p>
  84  * {@code Month} is an enum representing the 12 months of the year -
  85  * January, February, March, April, May, June, July, August, September, October,
  86  * November and December.
  87  * <p>
  88  * In addition to the textual enum name, each month-of-year has an {@code int} value.
  89  * The {@code int} value follows normal usage and the ISO-8601 standard,
  90  * from 1 (January) to 12 (December). It is recommended that applications use the enum
  91  * rather than the {@code int} value to ensure code clarity.


 175      * <p>
 176      * {@code Month} is an enum representing the 12 months of the year.
 177      * This factory allows the enum to be obtained from the {@code int} value.
 178      * The {@code int} value follows the ISO-8601 standard, from 1 (January) to 12 (December).
 179      *
 180      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 181      * @return the month-of-year, not null
 182      * @throws DateTimeException if the month-of-year is invalid
 183      */
 184     public static Month of(int month) {
 185         if (month < 1 || month > 12) {
 186             throw new DateTimeException("Invalid value for MonthOfYear: " + month);
 187         }
 188         return ENUMS[month - 1];
 189     }
 190 
 191     //-----------------------------------------------------------------------
 192     /**
 193      * Obtains an instance of {@code Month} from a temporal object.
 194      * <p>
 195      * A {@code TemporalAccessor} represents some form of date and time information.
 196      * This factory converts the arbitrary temporal object to an instance of {@code Month}.

 197      * <p>
 198      * The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} field.
 199      * The extraction is only permitted if the temporal object has an ISO
 200      * chronology, or can be converted to a {@code LocalDate}.
 201      * <p>
 202      * This method matches the signature of the functional interface {@link TemporalQuery}
 203      * allowing it to be used in queries via method reference, {@code Month::from}.
 204      *
 205      * @param temporal  the temporal object to convert, not null
 206      * @return the month-of-year, not null
 207      * @throws DateTimeException if unable to convert to a {@code Month}
 208      */
 209     public static Month from(TemporalAccessor temporal) {
 210         if (temporal instanceof Month) {
 211             return (Month) temporal;
 212         }
 213         try {
 214             if (ISOChrono.INSTANCE.equals(Chrono.from(temporal)) == false) {
 215                 temporal = LocalDate.from(temporal);
 216             }
 217             return of(temporal.get(MONTH_OF_YEAR));
 218         } catch (DateTimeException ex) {
 219             throw new DateTimeException("Unable to obtain Month from TemporalAccessor: " + temporal.getClass(), ex);
 220         }
 221     }
 222 
 223     //-----------------------------------------------------------------------
 224     /**
 225      * Gets the month-of-year {@code int} value.
 226      * <p>
 227      * The values are numbered following the ISO-8601 standard,
 228      * from 1 (January) to 12 (December).
 229      *
 230      * @return the month-of-year, from 1 (January) to 12 (December)
 231      */
 232     public int getValue() {
 233         return ordinal() + 1;
 234     }
 235 
 236     //-----------------------------------------------------------------------
 237     /**
 238      * Gets the textual representation, such as 'Jan' or 'December'.
 239      * <p>
 240      * This returns the textual name used to identify the month-of-year.
 241      * The parameters control the length of the returned text and the locale.

 242      * <p>
 243      * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
 244      *
 245      * @param style  the length of the text required, not null
 246      * @param locale  the locale to use, not null
 247      * @return the text value of the month-of-year, not null
 248      */
 249     public String getText(TextStyle style, Locale locale) {
 250         return new DateTimeFormatterBuilder().appendText(MONTH_OF_YEAR, style).toFormatter(locale).print(this);
 251     }
 252 
 253     //-----------------------------------------------------------------------
 254     /**
 255      * Checks if the specified field is supported.
 256      * <p>
 257      * This checks if this month-of-year can be queried for the specified field.
 258      * If false, then calling the {@link #range(TemporalField) range} and
 259      * {@link #get(TemporalField) get} methods will throw an exception.
 260      * <p>
 261      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then
 262      * this method returns true.
 263      * All other {@code ChronoField} instances will return false.
 264      * <p>
 265      * If the field is not a {@code ChronoField}, then the result of this method
 266      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
 267      * passing {@code this} as the argument.
 268      * Whether the field is supported is determined by the field.
 269      *
 270      * @param field  the field to check, null returns false
 271      * @return true if the field is supported on this month-of-year, false if not
 272      */
 273     @Override
 274     public boolean isSupported(TemporalField field) {
 275         if (field instanceof ChronoField) {
 276             return field == MONTH_OF_YEAR;
 277         }
 278         return field != null && field.doIsSupported(this);
 279     }
 280 
 281     /**
 282      * Gets the range of valid values for the specified field.
 283      * <p>
 284      * The range object expresses the minimum and maximum valid values for a field.
 285      * This month is used to enhance the accuracy of the returned range.
 286      * If it is not possible to return the range, because the field is not supported
 287      * or for some other reason, an exception is thrown.
 288      * <p>
 289      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
 290      * range of the month-of-year, from 1 to 12, will be returned.
 291      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 292      * <p>
 293      * If the field is not a {@code ChronoField}, then the result of this method
 294      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
 295      * passing {@code this} as the argument.
 296      * Whether the range can be obtained is determined by the field.
 297      *
 298      * @param field  the field to query the range for, not null
 299      * @return the range of valid values for the field, not null
 300      * @throws DateTimeException if the range for the field cannot be obtained
 301      */
 302     @Override
 303     public ValueRange range(TemporalField field) {
 304         if (field == MONTH_OF_YEAR) {
 305             return field.range();
 306         }
 307         return TemporalAccessor.super.range(field);
 308     }
 309 
 310     /**
 311      * Gets the value of the specified field from this month-of-year as an {@code int}.
 312      * <p>
 313      * This queries this month for the value for the specified field.
 314      * The returned value will always be within the valid range of values for the field.
 315      * If it is not possible to return the value, because the field is not supported
 316      * or for some other reason, an exception is thrown.
 317      * <p>
 318      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
 319      * value of the month-of-year, from 1 to 12, will be returned.
 320      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 321      * <p>
 322      * If the field is not a {@code ChronoField}, then the result of this method
 323      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 324      * passing {@code this} as the argument. Whether the value can be obtained,
 325      * and what the value represents, is determined by the field.
 326      *
 327      * @param field  the field to get, not null
 328      * @return the value for the field, within the valid range of values
 329      * @throws DateTimeException if a value for the field cannot be obtained
 330      * @throws DateTimeException if the range of valid values for the field exceeds an {@code int}
 331      * @throws DateTimeException if the value is outside the range of valid values for the field
 332      * @throws ArithmeticException if numeric overflow occurs
 333      */
 334     @Override
 335     public int get(TemporalField field) {
 336         if (field == MONTH_OF_YEAR) {
 337             return getValue();
 338         }
 339         return TemporalAccessor.super.get(field);
 340     }
 341 
 342     /**
 343      * Gets the value of the specified field from this month-of-year as a {@code long}.
 344      * <p>
 345      * This queries this month for the value for the specified field.
 346      * If it is not possible to return the value, because the field is not supported
 347      * or for some other reason, an exception is thrown.
 348      * <p>
 349      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
 350      * value of the month-of-year, from 1 to 12, will be returned.
 351      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 352      * <p>
 353      * If the field is not a {@code ChronoField}, then the result of this method
 354      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 355      * passing {@code this} as the argument. Whether the value can be obtained,
 356      * and what the value represents, is determined by the field.
 357      *
 358      * @param field  the field to get, not null
 359      * @return the value for the field
 360      * @throws DateTimeException if a value for the field cannot be obtained
 361      * @throws ArithmeticException if numeric overflow occurs
 362      */
 363     @Override
 364     public long getLong(TemporalField field) {
 365         if (field == MONTH_OF_YEAR) {
 366             return getValue();
 367         } else if (field instanceof ChronoField) {
 368             throw new DateTimeException("Unsupported field: " + field.getName());
 369         }
 370         return field.doGet(this);
 371     }
 372 
 373     //-----------------------------------------------------------------------
 374     /**
 375      * Returns the month-of-year that is the specified number of quarters after this one.
 376      * <p>
 377      * The calculation rolls around the end of the year from December to January.
 378      * The specified period may be negative.
 379      * <p>
 380      * This instance is immutable and unaffected by this method call.
 381      *
 382      * @param months  the months to add, positive or negative
 383      * @return the resulting month, not null
 384      */
 385     public Month plus(long months) {
 386         int amount = (int) (months % 12);
 387         return ENUMS[(ordinal() + (amount + 12)) % 12];
 388     }
 389 
 390     /**


 537      * Queries this month-of-year using the specified query.
 538      * <p>
 539      * This queries this month-of-year using the specified query strategy object.
 540      * The {@code TemporalQuery} object defines the logic to be used to
 541      * obtain the result. Read the documentation of the query to understand
 542      * what the result of this method will be.
 543      * <p>
 544      * The result of this method is obtained by invoking the
 545      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 546      * specified query passing {@code this} as the argument.
 547      *
 548      * @param <R> the type of the result
 549      * @param query  the query to invoke, not null
 550      * @return the query result, null may be returned (defined by the query)
 551      * @throws DateTimeException if unable to query (defined by the query)
 552      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
 553      */
 554     @SuppressWarnings("unchecked")
 555     @Override
 556     public <R> R query(TemporalQuery<R> query) {
 557         if (query == Queries.chrono()) {
 558             return (R) ISOChrono.INSTANCE;
 559         } else if (query == Queries.precision()) {
 560             return (R) MONTHS;
 561         }
 562         return TemporalAccessor.super.query(query);
 563     }
 564 
 565     /**
 566      * Adjusts the specified temporal object to have this month-of-year.
 567      * <p>
 568      * This returns a temporal object of the same observable type as the input
 569      * with the month-of-year changed to be the same as this.
 570      * <p>
 571      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
 572      * passing {@link ChronoField#MONTH_OF_YEAR} as the field.
 573      * If the specified temporal object does not use the ISO calendar system then
 574      * a {@code DateTimeException} is thrown.
 575      * <p>
 576      * In most cases, it is clearer to reverse the calling pattern by using
 577      * {@link Temporal#with(TemporalAdjuster)}:
 578      * <pre>


 582      * </pre>
 583      * <p>
 584      * For example, given a date in May, the following are output:
 585      * <pre>
 586      *   dateInMay.with(JANUARY);    // four months earlier
 587      *   dateInMay.with(APRIL);      // one months earlier
 588      *   dateInMay.with(MAY);        // same date
 589      *   dateInMay.with(JUNE);       // one month later
 590      *   dateInMay.with(DECEMBER);   // seven months later
 591      * </pre>
 592      * <p>
 593      * This instance is immutable and unaffected by this method call.
 594      *
 595      * @param temporal  the target object to be adjusted, not null
 596      * @return the adjusted object, not null
 597      * @throws DateTimeException if unable to make the adjustment
 598      * @throws ArithmeticException if numeric overflow occurs
 599      */
 600     @Override
 601     public Temporal adjustInto(Temporal temporal) {
 602         if (Chrono.from(temporal).equals(ISOChrono.INSTANCE) == false) {
 603             throw new DateTimeException("Adjustment only supported on ISO date-time");
 604         }
 605         return temporal.with(MONTH_OF_YEAR, getValue());
 606     }
 607 
 608 }


  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;
  63 
  64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  65 import static java.time.temporal.ChronoUnit.MONTHS;
  66 
  67 import java.time.chrono.Chronology;
  68 import java.time.chrono.IsoChronology;
  69 import java.time.format.DateTimeFormatterBuilder;
  70 import java.time.format.TextStyle;

  71 import java.time.temporal.ChronoField;

  72 import java.time.temporal.Queries;
  73 import java.time.temporal.Temporal;
  74 import java.time.temporal.TemporalAccessor;
  75 import java.time.temporal.TemporalAdjuster;
  76 import java.time.temporal.TemporalField;
  77 import java.time.temporal.TemporalQuery;
  78 import java.time.temporal.ValueRange;
  79 import java.util.Locale;
  80 
  81 /**
  82  * A month-of-year, such as 'July'.
  83  * <p>
  84  * {@code Month} is an enum representing the 12 months of the year -
  85  * January, February, March, April, May, June, July, August, September, October,
  86  * November and December.
  87  * <p>
  88  * In addition to the textual enum name, each month-of-year has an {@code int} value.
  89  * The {@code int} value follows normal usage and the ISO-8601 standard,
  90  * from 1 (January) to 12 (December). It is recommended that applications use the enum
  91  * rather than the {@code int} value to ensure code clarity.


 175      * <p>
 176      * {@code Month} is an enum representing the 12 months of the year.
 177      * This factory allows the enum to be obtained from the {@code int} value.
 178      * The {@code int} value follows the ISO-8601 standard, from 1 (January) to 12 (December).
 179      *
 180      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 181      * @return the month-of-year, not null
 182      * @throws DateTimeException if the month-of-year is invalid
 183      */
 184     public static Month of(int month) {
 185         if (month < 1 || month > 12) {
 186             throw new DateTimeException("Invalid value for MonthOfYear: " + month);
 187         }
 188         return ENUMS[month - 1];
 189     }
 190 
 191     //-----------------------------------------------------------------------
 192     /**
 193      * Obtains an instance of {@code Month} from a temporal object.
 194      * <p>
 195      * This obtains a month based on the specified temporal.
 196      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 197      * which this factory converts to an instance of {@code Month}.
 198      * <p>
 199      * The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} field.
 200      * The extraction is only permitted if the temporal object has an ISO
 201      * chronology, or can be converted to a {@code LocalDate}.
 202      * <p>
 203      * This method matches the signature of the functional interface {@link TemporalQuery}
 204      * allowing it to be used in queries via method reference, {@code Month::from}.
 205      *
 206      * @param temporal  the temporal object to convert, not null
 207      * @return the month-of-year, not null
 208      * @throws DateTimeException if unable to convert to a {@code Month}
 209      */
 210     public static Month from(TemporalAccessor temporal) {
 211         if (temporal instanceof Month) {
 212             return (Month) temporal;
 213         }
 214         try {
 215             if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {
 216                 temporal = LocalDate.from(temporal);
 217             }
 218             return of(temporal.get(MONTH_OF_YEAR));
 219         } catch (DateTimeException ex) {
 220             throw new DateTimeException("Unable to obtain Month from TemporalAccessor: " + temporal.getClass(), ex);
 221         }
 222     }
 223 
 224     //-----------------------------------------------------------------------
 225     /**
 226      * Gets the month-of-year {@code int} value.
 227      * <p>
 228      * The values are numbered following the ISO-8601 standard,
 229      * from 1 (January) to 12 (December).
 230      *
 231      * @return the month-of-year, from 1 (January) to 12 (December)
 232      */
 233     public int getValue() {
 234         return ordinal() + 1;
 235     }
 236 
 237     //-----------------------------------------------------------------------
 238     /**
 239      * Gets the textual representation, such as 'Jan' or 'December'.
 240      * <p>
 241      * This returns the textual name used to identify the month-of-year,
 242      * suitable for presentation to the user.
 243      * The parameters control the style of the returned text and the locale.
 244      * <p>
 245      * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
 246      *
 247      * @param style  the length of the text required, not null
 248      * @param locale  the locale to use, not null
 249      * @return the text value of the month-of-year, not null
 250      */
 251     public String getDisplayName(TextStyle style, Locale locale) {
 252         return new DateTimeFormatterBuilder().appendText(MONTH_OF_YEAR, style).toFormatter(locale).format(this);
 253     }
 254 
 255     //-----------------------------------------------------------------------
 256     /**
 257      * Checks if the specified field is supported.
 258      * <p>
 259      * This checks if this month-of-year can be queried for the specified field.
 260      * If false, then calling the {@link #range(TemporalField) range} and
 261      * {@link #get(TemporalField) get} methods will throw an exception.
 262      * <p>
 263      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then
 264      * this method returns true.
 265      * All other {@code ChronoField} instances will return false.
 266      * <p>
 267      * If the field is not a {@code ChronoField}, then the result of this method
 268      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 269      * passing {@code this} as the argument.
 270      * Whether the field is supported is determined by the field.
 271      *
 272      * @param field  the field to check, null returns false
 273      * @return true if the field is supported on this month-of-year, false if not
 274      */
 275     @Override
 276     public boolean isSupported(TemporalField field) {
 277         if (field instanceof ChronoField) {
 278             return field == MONTH_OF_YEAR;
 279         }
 280         return field != null && field.isSupportedBy(this);
 281     }
 282 
 283     /**
 284      * Gets the range of valid values for the specified field.
 285      * <p>
 286      * The range object expresses the minimum and maximum valid values for a field.
 287      * This month is used to enhance the accuracy of the returned range.
 288      * If it is not possible to return the range, because the field is not supported
 289      * or for some other reason, an exception is thrown.
 290      * <p>
 291      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
 292      * range of the month-of-year, from 1 to 12, will be returned.
 293      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 294      * <p>
 295      * If the field is not a {@code ChronoField}, then the result of this method
 296      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 297      * passing {@code this} as the argument.
 298      * Whether the range can be obtained is determined by the field.
 299      *
 300      * @param field  the field to query the range for, not null
 301      * @return the range of valid values for the field, not null
 302      * @throws DateTimeException if the range for the field cannot be obtained
 303      */
 304     @Override
 305     public ValueRange range(TemporalField field) {
 306         if (field == MONTH_OF_YEAR) {
 307             return field.range();
 308         }
 309         return TemporalAccessor.super.range(field);
 310     }
 311 
 312     /**
 313      * Gets the value of the specified field from this month-of-year as an {@code int}.
 314      * <p>
 315      * This queries this month for the value for the specified field.
 316      * The returned value will always be within the valid range of values for the field.
 317      * If it is not possible to return the value, because the field is not supported
 318      * or for some other reason, an exception is thrown.
 319      * <p>
 320      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
 321      * value of the month-of-year, from 1 to 12, will be returned.
 322      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 323      * <p>
 324      * If the field is not a {@code ChronoField}, then the result of this method
 325      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 326      * passing {@code this} as the argument. Whether the value can be obtained,
 327      * and what the value represents, is determined by the field.
 328      *
 329      * @param field  the field to get, not null
 330      * @return the value for the field, within the valid range of values
 331      * @throws DateTimeException if a value for the field cannot be obtained


 332      * @throws ArithmeticException if numeric overflow occurs
 333      */
 334     @Override
 335     public int get(TemporalField field) {
 336         if (field == MONTH_OF_YEAR) {
 337             return getValue();
 338         }
 339         return TemporalAccessor.super.get(field);
 340     }
 341 
 342     /**
 343      * Gets the value of the specified field from this month-of-year as a {@code long}.
 344      * <p>
 345      * This queries this month for the value for the specified field.
 346      * If it is not possible to return the value, because the field is not supported
 347      * or for some other reason, an exception is thrown.
 348      * <p>
 349      * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
 350      * value of the month-of-year, from 1 to 12, will be returned.
 351      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 352      * <p>
 353      * If the field is not a {@code ChronoField}, then the result of this method
 354      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 355      * passing {@code this} as the argument. Whether the value can be obtained,
 356      * and what the value represents, is determined by the field.
 357      *
 358      * @param field  the field to get, not null
 359      * @return the value for the field
 360      * @throws DateTimeException if a value for the field cannot be obtained
 361      * @throws ArithmeticException if numeric overflow occurs
 362      */
 363     @Override
 364     public long getLong(TemporalField field) {
 365         if (field == MONTH_OF_YEAR) {
 366             return getValue();
 367         } else if (field instanceof ChronoField) {
 368             throw new DateTimeException("Unsupported field: " + field.getName());
 369         }
 370         return field.getFrom(this);
 371     }
 372 
 373     //-----------------------------------------------------------------------
 374     /**
 375      * Returns the month-of-year that is the specified number of quarters after this one.
 376      * <p>
 377      * The calculation rolls around the end of the year from December to January.
 378      * The specified period may be negative.
 379      * <p>
 380      * This instance is immutable and unaffected by this method call.
 381      *
 382      * @param months  the months to add, positive or negative
 383      * @return the resulting month, not null
 384      */
 385     public Month plus(long months) {
 386         int amount = (int) (months % 12);
 387         return ENUMS[(ordinal() + (amount + 12)) % 12];
 388     }
 389 
 390     /**


 537      * Queries this month-of-year using the specified query.
 538      * <p>
 539      * This queries this month-of-year using the specified query strategy object.
 540      * The {@code TemporalQuery} object defines the logic to be used to
 541      * obtain the result. Read the documentation of the query to understand
 542      * what the result of this method will be.
 543      * <p>
 544      * The result of this method is obtained by invoking the
 545      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 546      * specified query passing {@code this} as the argument.
 547      *
 548      * @param <R> the type of the result
 549      * @param query  the query to invoke, not null
 550      * @return the query result, null may be returned (defined by the query)
 551      * @throws DateTimeException if unable to query (defined by the query)
 552      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
 553      */
 554     @SuppressWarnings("unchecked")
 555     @Override
 556     public <R> R query(TemporalQuery<R> query) {
 557         if (query == Queries.chronology()) {
 558             return (R) IsoChronology.INSTANCE;
 559         } else if (query == Queries.precision()) {
 560             return (R) MONTHS;
 561         }
 562         return TemporalAccessor.super.query(query);
 563     }
 564 
 565     /**
 566      * Adjusts the specified temporal object to have this month-of-year.
 567      * <p>
 568      * This returns a temporal object of the same observable type as the input
 569      * with the month-of-year changed to be the same as this.
 570      * <p>
 571      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
 572      * passing {@link ChronoField#MONTH_OF_YEAR} as the field.
 573      * If the specified temporal object does not use the ISO calendar system then
 574      * a {@code DateTimeException} is thrown.
 575      * <p>
 576      * In most cases, it is clearer to reverse the calling pattern by using
 577      * {@link Temporal#with(TemporalAdjuster)}:
 578      * <pre>


 582      * </pre>
 583      * <p>
 584      * For example, given a date in May, the following are output:
 585      * <pre>
 586      *   dateInMay.with(JANUARY);    // four months earlier
 587      *   dateInMay.with(APRIL);      // one months earlier
 588      *   dateInMay.with(MAY);        // same date
 589      *   dateInMay.with(JUNE);       // one month later
 590      *   dateInMay.with(DECEMBER);   // seven months later
 591      * </pre>
 592      * <p>
 593      * This instance is immutable and unaffected by this method call.
 594      *
 595      * @param temporal  the target object to be adjusted, not null
 596      * @return the adjusted object, not null
 597      * @throws DateTimeException if unable to make the adjustment
 598      * @throws ArithmeticException if numeric overflow occurs
 599      */
 600     @Override
 601     public Temporal adjustInto(Temporal temporal) {
 602         if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {
 603             throw new DateTimeException("Adjustment only supported on ISO date-time");
 604         }
 605         return temporal.with(MONTH_OF_YEAR, getValue());
 606     }
 607 
 608 }