src/share/classes/java/time/Month.java
Print this page
*** 62,76 ****
package java.time;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoUnit.MONTHS;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
- import java.time.temporal.Chrono;
import java.time.temporal.ChronoField;
- import java.time.temporal.ISOChrono;
import java.time.temporal.Queries;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalField;
--- 62,76 ----
package java.time;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoUnit.MONTHS;
+ import java.time.chrono.Chronology;
+ import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.Queries;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalField;
*** 190,201 ****
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code Month} from a temporal object.
* <p>
! * A {@code TemporalAccessor} represents some form of date and time information.
! * This factory converts the arbitrary temporal object to an instance of {@code Month}.
* <p>
* The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} field.
* The extraction is only permitted if the temporal object has an ISO
* chronology, or can be converted to a {@code LocalDate}.
* <p>
--- 190,202 ----
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code Month} from a temporal object.
* <p>
! * This obtains a month based on the specified temporal.
! * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
! * which this factory converts to an instance of {@code Month}.
* <p>
* The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} field.
* The extraction is only permitted if the temporal object has an ISO
* chronology, or can be converted to a {@code LocalDate}.
* <p>
*** 209,219 ****
public static Month from(TemporalAccessor temporal) {
if (temporal instanceof Month) {
return (Month) temporal;
}
try {
! if (ISOChrono.INSTANCE.equals(Chrono.from(temporal)) == false) {
temporal = LocalDate.from(temporal);
}
return of(temporal.get(MONTH_OF_YEAR));
} catch (DateTimeException ex) {
throw new DateTimeException("Unable to obtain Month from TemporalAccessor: " + temporal.getClass(), ex);
--- 210,220 ----
public static Month from(TemporalAccessor temporal) {
if (temporal instanceof Month) {
return (Month) temporal;
}
try {
! if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {
temporal = LocalDate.from(temporal);
}
return of(temporal.get(MONTH_OF_YEAR));
} catch (DateTimeException ex) {
throw new DateTimeException("Unable to obtain Month from TemporalAccessor: " + temporal.getClass(), ex);
*** 235,255 ****
//-----------------------------------------------------------------------
/**
* Gets the textual representation, such as 'Jan' or 'December'.
* <p>
! * This returns the textual name used to identify the month-of-year.
! * The parameters control the length of the returned text and the locale.
* <p>
* If no textual mapping is found then the {@link #getValue() numeric value} is returned.
*
* @param style the length of the text required, not null
* @param locale the locale to use, not null
* @return the text value of the month-of-year, not null
*/
! public String getText(TextStyle style, Locale locale) {
! return new DateTimeFormatterBuilder().appendText(MONTH_OF_YEAR, style).toFormatter(locale).print(this);
}
//-----------------------------------------------------------------------
/**
* Checks if the specified field is supported.
--- 236,257 ----
//-----------------------------------------------------------------------
/**
* Gets the textual representation, such as 'Jan' or 'December'.
* <p>
! * This returns the textual name used to identify the month-of-year,
! * suitable for presentation to the user.
! * The parameters control the style of the returned text and the locale.
* <p>
* If no textual mapping is found then the {@link #getValue() numeric value} is returned.
*
* @param style the length of the text required, not null
* @param locale the locale to use, not null
* @return the text value of the month-of-year, not null
*/
! public String getDisplayName(TextStyle style, Locale locale) {
! return new DateTimeFormatterBuilder().appendText(MONTH_OF_YEAR, style).toFormatter(locale).format(this);
}
//-----------------------------------------------------------------------
/**
* Checks if the specified field is supported.
*** 261,271 ****
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then
* this method returns true.
* All other {@code ChronoField} instances will return false.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the field is supported is determined by the field.
*
* @param field the field to check, null returns false
* @return true if the field is supported on this month-of-year, false if not
--- 263,273 ----
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then
* this method returns true.
* All other {@code ChronoField} instances will return false.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the field is supported is determined by the field.
*
* @param field the field to check, null returns false
* @return true if the field is supported on this month-of-year, false if not
*** 273,283 ****
@Override
public boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
return field == MONTH_OF_YEAR;
}
! return field != null && field.doIsSupported(this);
}
/**
* Gets the range of valid values for the specified field.
* <p>
--- 275,285 ----
@Override
public boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
return field == MONTH_OF_YEAR;
}
! return field != null && field.isSupportedBy(this);
}
/**
* Gets the range of valid values for the specified field.
* <p>
*** 289,299 ****
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
* range of the month-of-year, from 1 to 12, will be returned.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the range can be obtained is determined by the field.
*
* @param field the field to query the range for, not null
* @return the range of valid values for the field, not null
--- 291,301 ----
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
* range of the month-of-year, from 1 to 12, will be returned.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the range can be obtained is determined by the field.
*
* @param field the field to query the range for, not null
* @return the range of valid values for the field, not null
*** 318,336 ****
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
* value of the month-of-year, from 1 to 12, will be returned.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field, within the valid range of values
* @throws DateTimeException if a value for the field cannot be obtained
- * @throws DateTimeException if the range of valid values for the field exceeds an {@code int}
- * @throws DateTimeException if the value is outside the range of valid values for the field
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public int get(TemporalField field) {
if (field == MONTH_OF_YEAR) {
--- 320,336 ----
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
* value of the month-of-year, from 1 to 12, will be returned.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field, within the valid range of values
* @throws DateTimeException if a value for the field cannot be obtained
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public int get(TemporalField field) {
if (field == MONTH_OF_YEAR) {
*** 349,359 ****
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
* value of the month-of-year, from 1 to 12, will be returned.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field
--- 349,359 ----
* If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the
* value of the month-of-year, from 1 to 12, will be returned.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field
*** 365,375 ****
if (field == MONTH_OF_YEAR) {
return getValue();
} else if (field instanceof ChronoField) {
throw new DateTimeException("Unsupported field: " + field.getName());
}
! return field.doGet(this);
}
//-----------------------------------------------------------------------
/**
* Returns the month-of-year that is the specified number of quarters after this one.
--- 365,375 ----
if (field == MONTH_OF_YEAR) {
return getValue();
} else if (field instanceof ChronoField) {
throw new DateTimeException("Unsupported field: " + field.getName());
}
! return field.getFrom(this);
}
//-----------------------------------------------------------------------
/**
* Returns the month-of-year that is the specified number of quarters after this one.
*** 552,563 ****
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
@SuppressWarnings("unchecked")
@Override
public <R> R query(TemporalQuery<R> query) {
! if (query == Queries.chrono()) {
! return (R) ISOChrono.INSTANCE;
} else if (query == Queries.precision()) {
return (R) MONTHS;
}
return TemporalAccessor.super.query(query);
}
--- 552,563 ----
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
@SuppressWarnings("unchecked")
@Override
public <R> R query(TemporalQuery<R> query) {
! if (query == Queries.chronology()) {
! return (R) IsoChronology.INSTANCE;
} else if (query == Queries.precision()) {
return (R) MONTHS;
}
return TemporalAccessor.super.query(query);
}
*** 597,607 ****
* @throws DateTimeException if unable to make the adjustment
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public Temporal adjustInto(Temporal temporal) {
! if (Chrono.from(temporal).equals(ISOChrono.INSTANCE) == false) {
throw new DateTimeException("Adjustment only supported on ISO date-time");
}
return temporal.with(MONTH_OF_YEAR, getValue());
}
--- 597,607 ----
* @throws DateTimeException if unable to make the adjustment
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public Temporal adjustInto(Temporal temporal) {
! if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {
throw new DateTimeException("Adjustment only supported on ISO date-time");
}
return temporal.with(MONTH_OF_YEAR, getValue());
}