src/share/classes/java/time/YearMonth.java
Print this page
*** 59,71 ****
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time;
- import static java.time.temporal.ChronoField.EPOCH_MONTH;
import static java.time.temporal.ChronoField.ERA;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
import static java.time.temporal.ChronoUnit.MONTHS;
import java.io.DataInput;
--- 59,71 ----
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time;
import static java.time.temporal.ChronoField.ERA;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
+ import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
import static java.time.temporal.ChronoField.YEAR;
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
import static java.time.temporal.ChronoUnit.MONTHS;
import java.io.DataInput;
*** 80,97 ****
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
- import java.time.temporal.Queries;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;
import java.time.temporal.ValueRange;
import java.util.Objects;
/**
* A year-month in the ISO-8601 calendar system, such as {@code 2007-12}.
--- 80,97 ----
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;
+ import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.temporal.ValueRange;
import java.util.Objects;
/**
* A year-month in the ISO-8601 calendar system, such as {@code 2007-12}.
*** 253,263 ****
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code YearMonth} from a text string such as {@code 2007-12}.
* <p>
* The string must represent a valid year-month.
! * The format must be {@code yyyy-MM}.
* Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
*
* @param text the text to parse such as "2007-12", not null
* @return the parsed year-month, not null
* @throws DateTimeParseException if the text cannot be parsed
--- 253,263 ----
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code YearMonth} from a text string such as {@code 2007-12}.
* <p>
* The string must represent a valid year-month.
! * The format must be {@code uuuu-MM}.
* Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
*
* @param text the text to parse such as "2007-12", not null
* @return the parsed year-month, not null
* @throws DateTimeParseException if the text cannot be parsed
*** 318,328 ****
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The supported fields are:
* <ul>
* <li>{@code MONTH_OF_YEAR}
! * <li>{@code EPOCH_MONTH}
* <li>{@code YEAR_OF_ERA}
* <li>{@code YEAR}
* <li>{@code ERA}
* </ul>
* All other {@code ChronoField} instances will return false.
--- 318,328 ----
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The supported fields are:
* <ul>
* <li>{@code MONTH_OF_YEAR}
! * <li>{@code PROLEPTIC_MONTH}
* <li>{@code YEAR_OF_ERA}
* <li>{@code YEAR}
* <li>{@code ERA}
* </ul>
* All other {@code ChronoField} instances will return false.
*** 337,347 ****
*/
@Override
public boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
return field == YEAR || field == MONTH_OF_YEAR ||
! field == EPOCH_MONTH || field == YEAR_OF_ERA || field == ERA;
}
return field != null && field.isSupportedBy(this);
}
/**
--- 337,347 ----
*/
@Override
public boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
return field == YEAR || field == MONTH_OF_YEAR ||
! field == PROLEPTIC_MONTH || field == YEAR_OF_ERA || field == ERA;
}
return field != null && field.isSupportedBy(this);
}
/**
*** 353,372 ****
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return
* appropriate range instances.
! * 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
* @throws DateTimeException if the range for the field cannot be obtained
*/
@Override
public ValueRange range(TemporalField field) {
if (field == YEAR_OF_ERA) {
return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
--- 353,373 ----
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return
* appropriate range instances.
! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <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
* @throws DateTimeException if the range for the field cannot be obtained
+ * @throws UnsupportedTemporalTypeException if the field is not supported
*/
@Override
public ValueRange range(TemporalField field) {
if (field == YEAR_OF_ERA) {
return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
*** 382,403 ****
* If it is not possible to return the value, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
! * values based on this year-month, except {@code EPOCH_MONTH} which is too
* large to fit in an {@code int} and throw a {@code DateTimeException}.
! * 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
! * @throws DateTimeException if a value for the field cannot be obtained
* @throws ArithmeticException if numeric overflow occurs
*/
@Override // override for Javadoc
public int get(TemporalField field) {
return range(field).checkValidIntValue(getLong(field), field);
--- 383,407 ----
* If it is not possible to return the value, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
! * values based on this year-month, except {@code PROLEPTIC_MONTH} which is too
* large to fit in an {@code int} and throw a {@code DateTimeException}.
! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <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
! * @throws DateTimeException if a value for the field cannot be obtained or
! * the value is outside the range of valid values for the field
! * @throws UnsupportedTemporalTypeException if the field is not supported or
! * the range of values exceeds an {@code int}
* @throws ArithmeticException if numeric overflow occurs
*/
@Override // override for Javadoc
public int get(TemporalField field) {
return range(field).checkValidIntValue(getLong(field), field);
*** 411,449 ****
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
* values based on this year-month.
! * 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
* @throws DateTimeException if a value for the field cannot be obtained
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case MONTH_OF_YEAR: return month;
! case EPOCH_MONTH: return getEpochMonth();
case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
case YEAR: return year;
case ERA: return (year < 1 ? 0 : 1);
}
! throw new DateTimeException("Unsupported field: " + field.getName());
}
return field.getFrom(this);
}
! private long getEpochMonth() {
! return ((year - 1970) * 12L) + (month - 1);
}
//-----------------------------------------------------------------------
/**
* Gets the year field.
--- 415,454 ----
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
* values based on this year-month.
! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <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
* @throws DateTimeException if a value for the field cannot be obtained
+ * @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case MONTH_OF_YEAR: return month;
! case PROLEPTIC_MONTH: return getProlepticMonth();
case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
case YEAR: return year;
case ERA: return (year < 1 ? 0 : 1);
}
! throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
}
return field.getFrom(this);
}
! private long getProlepticMonth() {
! return (year * 12L + month - 1);
}
//-----------------------------------------------------------------------
/**
* Gets the year field.
*** 587,598 ****
* The supported fields behave as follows:
* <ul>
* <li>{@code MONTH_OF_YEAR} -
* Returns a {@code YearMonth} with the specified month-of-year.
* The year will be unchanged.
! * <li>{@code EPOCH_MONTH} -
! * Returns a {@code YearMonth} with the specified epoch-month.
* This completely replaces the year and month of this object.
* <li>{@code YEAR_OF_ERA} -
* Returns a {@code YearMonth} with the specified year-of-era
* The month and era will be unchanged.
* <li>{@code YEAR} -
--- 592,603 ----
* The supported fields behave as follows:
* <ul>
* <li>{@code MONTH_OF_YEAR} -
* Returns a {@code YearMonth} with the specified month-of-year.
* The year will be unchanged.
! * <li>{@code PROLEPTIC_MONTH} -
! * Returns a {@code YearMonth} with the specified proleptic-month.
* This completely replaces the year and month of this object.
* <li>{@code YEAR_OF_ERA} -
* Returns a {@code YearMonth} with the specified year-of-era
* The month and era will be unchanged.
* <li>{@code YEAR} -
*** 604,614 ****
* </ul>
* <p>
* In all cases, if the new value is outside the valid range of values for the field
* then a {@code DateTimeException} will be thrown.
* <p>
! * 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.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
--- 609,619 ----
* </ul>
* <p>
* In all cases, if the new value is outside the valid range of values for the field
* then a {@code DateTimeException} will be thrown.
* <p>
! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
*** 617,641 ****
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code YearMonth} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public YearMonth with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
f.checkValidValue(newValue);
switch (f) {
case MONTH_OF_YEAR: return withMonth((int) newValue);
! case EPOCH_MONTH: return plusMonths(newValue - getLong(EPOCH_MONTH));
case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : newValue));
case YEAR: return withYear((int) newValue);
case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
}
! throw new DateTimeException("Unsupported field: " + field.getName());
}
return field.adjustInto(this, newValue);
}
//-----------------------------------------------------------------------
--- 622,647 ----
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code YearMonth} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
+ * @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public YearMonth with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
f.checkValidValue(newValue);
switch (f) {
case MONTH_OF_YEAR: return withMonth((int) newValue);
! case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : newValue));
case YEAR: return withYear((int) newValue);
case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
}
! throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
}
return field.adjustInto(this, newValue);
}
//-----------------------------------------------------------------------
*** 726,736 ****
* Only two eras are supported so the amount must be one, zero or minus one.
* If the amount is non-zero then the year is changed such that the year-of-era
* is unchanged.
* </ul>
* <p>
! * All other {@code ChronoUnit} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoUnit}, then the result of this method
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
* passing {@code this} as the argument. In this case, the unit determines
* whether and how to perform the addition.
--- 732,742 ----
* Only two eras are supported so the amount must be one, zero or minus one.
* If the amount is non-zero then the year is changed such that the year-of-era
* is unchanged.
* </ul>
* <p>
! * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoUnit}, then the result of this method
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
* passing {@code this} as the argument. In this case, the unit determines
* whether and how to perform the addition.
*** 739,748 ****
--- 745,755 ----
*
* @param amountToAdd the amount of the unit to add to the result, may be negative
* @param unit the unit of the amount to add, not null
* @return a {@code YearMonth} based on this year-month with the specified amount added, not null
* @throws DateTimeException if the addition cannot be made
+ * @throws UnsupportedTemporalTypeException if the unit is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public YearMonth plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
*** 752,762 ****
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
}
! throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.addTo(this, amountToAdd);
}
/**
--- 759,769 ----
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
}
! throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName());
}
return unit.addTo(this, amountToAdd);
}
/**
*** 836,845 ****
--- 843,853 ----
*
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative
* @param unit the unit of the amount to subtract, not null
* @return a {@code YearMonth} based on this year-month with the specified amount subtracted, not null
* @throws DateTimeException if the subtraction cannot be made
+ * @throws UnsupportedTemporalTypeException if the unit is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public YearMonth minus(long amountToSubtract, TemporalUnit unit) {
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
*** 891,903 ****
* @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 Temporal.super.query(query);
}
--- 899,911 ----
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
@SuppressWarnings("unchecked")
@Override
public <R> R query(TemporalQuery<R> query) {
! if (query == TemporalQuery.chronology()) {
return (R) IsoChronology.INSTANCE;
! } else if (query == TemporalQuery.precision()) {
return (R) MONTHS;
}
return Temporal.super.query(query);
}
*** 906,916 ****
* <p>
* This returns a temporal object of the same observable type as the input
* with the year and month changed to be the same as this.
* <p>
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
! * passing {@link ChronoField#EPOCH_MONTH} as the field.
* If the specified temporal object does not use the ISO calendar system then
* a {@code DateTimeException} is thrown.
* <p>
* In most cases, it is clearer to reverse the calling pattern by using
* {@link Temporal#with(TemporalAdjuster)}:
--- 914,924 ----
* <p>
* This returns a temporal object of the same observable type as the input
* with the year and month changed to be the same as this.
* <p>
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
! * passing {@link ChronoField#PROLEPTIC_MONTH} as the field.
* If the specified temporal object does not use the ISO calendar system then
* a {@code DateTimeException} is thrown.
* <p>
* In most cases, it is clearer to reverse the calling pattern by using
* {@link Temporal#with(TemporalAdjuster)}:
*** 930,940 ****
@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(EPOCH_MONTH, getEpochMonth());
}
/**
* Calculates the period between this year-month and another year-month in
* terms of the specified unit.
--- 938,948 ----
@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(PROLEPTIC_MONTH, getProlepticMonth());
}
/**
* Calculates the period between this year-month and another year-month in
* terms of the specified unit.
*** 975,1008 ****
*
* @param endYearMonth the end year-month, which must be a {@code YearMonth}, not null
* @param unit the unit to measure the period in, not null
* @return the amount of the period between this year-month and the end year-month
* @throws DateTimeException if the period cannot be calculated
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public long periodUntil(Temporal endYearMonth, TemporalUnit unit) {
if (endYearMonth instanceof YearMonth == false) {
Objects.requireNonNull(endYearMonth, "endYearMonth");
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
YearMonth end = (YearMonth) endYearMonth;
if (unit instanceof ChronoUnit) {
! long monthsUntil = end.getEpochMonth() - getEpochMonth(); // no overflow
switch ((ChronoUnit) unit) {
case MONTHS: return monthsUntil;
case YEARS: return monthsUntil / 12;
case DECADES: return monthsUntil / 120;
case CENTURIES: return monthsUntil / 1200;
case MILLENNIA: return monthsUntil / 12000;
case ERAS: return end.getLong(ERA) - getLong(ERA);
}
! throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endYearMonth);
}
//-----------------------------------------------------------------------
/**
* Combines this year-month with a day-of-month to create a {@code LocalDate}.
* <p>
* This returns a {@code LocalDate} formed from this year-month and the specified day-of-month.
--- 983,1031 ----
*
* @param endYearMonth the end year-month, which must be a {@code YearMonth}, not null
* @param unit the unit to measure the period in, not null
* @return the amount of the period between this year-month and the end year-month
* @throws DateTimeException if the period cannot be calculated
+ * @throws UnsupportedTemporalTypeException if the unit is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public long periodUntil(Temporal endYearMonth, TemporalUnit unit) {
if (endYearMonth instanceof YearMonth == false) {
Objects.requireNonNull(endYearMonth, "endYearMonth");
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
YearMonth end = (YearMonth) endYearMonth;
if (unit instanceof ChronoUnit) {
! long monthsUntil = end.getProlepticMonth() - getProlepticMonth(); // no overflow
switch ((ChronoUnit) unit) {
case MONTHS: return monthsUntil;
case YEARS: return monthsUntil / 12;
case DECADES: return monthsUntil / 120;
case CENTURIES: return monthsUntil / 1200;
case MILLENNIA: return monthsUntil / 12000;
case ERAS: return end.getLong(ERA) - getLong(ERA);
}
! throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endYearMonth);
}
+ /**
+ * Formats this year-month using the specified formatter.
+ * <p>
+ * This year-month will be passed to the formatter to produce a string.
+ *
+ * @param formatter the formatter to use, not null
+ * @return the formatted year-month string, not null
+ * @throws DateTimeException if an error occurs during printing
+ */
+ public String format(DateTimeFormatter formatter) {
+ Objects.requireNonNull(formatter, "formatter");
+ return formatter.format(this);
+ }
+
//-----------------------------------------------------------------------
/**
* Combines this year-month with a day-of-month to create a {@code LocalDate}.
* <p>
* This returns a {@code LocalDate} formed from this year-month and the specified day-of-month.
*** 1113,1123 ****
//-----------------------------------------------------------------------
/**
* Outputs this year-month as a {@code String}, such as {@code 2007-12}.
* <p>
! * The output will be in the format {@code yyyy-MM}:
*
* @return a string representation of this year-month, not null
*/
@Override
public String toString() {
--- 1136,1146 ----
//-----------------------------------------------------------------------
/**
* Outputs this year-month as a {@code String}, such as {@code 2007-12}.
* <p>
! * The output will be in the format {@code uuuu-MM}:
*
* @return a string representation of this year-month, not null
*/
@Override
public String toString() {
*** 1135,1159 ****
return buf.append(month < 10 ? "-0" : "-")
.append(month)
.toString();
}
- /**
- * Outputs this year-month as a {@code String} using the formatter.
- * <p>
- * This year-month will be passed to the formatter
- * {@link DateTimeFormatter#format(TemporalAccessor) format method}.
- *
- * @param formatter the formatter to use, not null
- * @return the formatted year-month string, not null
- * @throws DateTimeException if an error occurs during printing
- */
- public String toString(DateTimeFormatter formatter) {
- Objects.requireNonNull(formatter, "formatter");
- return formatter.format(this);
- }
-
//-----------------------------------------------------------------------
/**
* Writes the object using a
* <a href="../../../serialized-form.html#java.time.temporal.Ser">dedicated serialized form</a>.
* <pre>
--- 1158,1167 ----