YearMonth
Extends:
A year-month in the ISO-8601 calendar system, such as {@code 2007-12}.
{@code YearMonth} is an immutable date-time object that represents the combination of a year and month. Any field that can be derived from a year and month, such as quarter-of-year, can be obtained.
This class does not store or represent a day, time or time-zone. For example, the value "October 2007" can be stored in a {@code YearMonth}.
The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable.
Specification for implementors
This class is immutable and thread-safe.Static Method Summary
| Static Public Methods | ||
| public static |
from(temporal: TemporalAccessor): YearMonth Obtains an instance of {@code YearMonth} from a temporal object. |
|
| public static |
function overloading for YearMonth.now |
|
| public static |
Obtains the current year-month from the system clock in the default time-zone. |
|
| public static |
Obtains the current year-month from the specified clock. |
|
| public static |
Obtains the current year-month from the system clock in the specified time-zone. |
|
| public static |
function overloading for YearMonth.of |
|
| public static |
ofNumberMonth(year: number, month: Month): YearMonth Obtains an instance of {@code YearMonth} from a year and month. |
|
| public static |
ofNumberNumber(year: number, month: number): YearMonth Obtains an instance of {@code YearMonth} from a year and month. |
|
| public static |
parse(arg1: String, arg2: DateTimeFormatter | null): MonthDay function overloading for YearMonth.parse |
|
| public static |
parseString(text: String): YearMonth Obtains an instance of {@code YearMonth} from a text string such as {@code 2007-12}. |
|
| public static |
parseStringFormatter(text: String, formatter: DateTimeFormatter): * Obtains an instance of {@code YearMonth} from a text string using a specific formatter. |
|
Constructor Summary
| Public Constructor | ||
| public |
constructor(year: number, month: number) Constructor. |
|
Method Summary
| Public Methods | ||
| public |
adjustInto(temporal: Temporal): Temporal Adjusts the specified temporal object to have this year-month. |
|
| public |
Combines this year-month with a day-of-month to create a {@code LocalDate}. |
|
| public |
Returns a {@code LocalDate} at the end of the month. |
|
| public |
Compares this year-month to another year-month. |
|
| public |
Checks if this year-month is equal to another year-month. |
|
| public |
format(formatter: DateTimeFormatter): String Outputs this year-month as a {@code String} using the formatter. |
|
| public |
get(field: TemporalField): number Gets the value of the specified field from this year-month as an {@code int}. |
|
| public |
getLong(field: TemporalField): number Gets the value of the specified field from this year-month as a {@code long}. |
|
| public |
Is this year-month after the specified year-month. |
|
| public |
Is this year-month before the specified year-month. |
|
| public |
Checks if the year is a leap year, according to the ISO proleptic calendar system rules. |
|
| public |
isSupported(arg1: TemporalField | ChronoUnit): boolean function overloading for YearMonth.isSupported |
|
| public |
isSupportedField(field: TemporalField): boolean Checks if the specified field is supported. |
|
| public |
isSupportedUnit(unit: *): * |
|
| public |
isValidDay(dayOfMonth: number): boolean Checks if the day-of-month is valid for this year-month. |
|
| public |
Returns the length of the month, taking account of the year. |
|
| public |
Returns the length of the year. |
|
| public |
minus(amount: TemporalAmount | number, unit: TemporalUnit): YearMonth function overloading for YearMonth.minus |
|
| public |
minusAmount(amount: TemporalAmount): YearMonth Returns a copy of this year-month with the specified period subtracted. |
|
| public |
minusAmountUnit(amountToSubtract: number, unit: TemporalUnit): YearMonth |
|
| public |
minusMonths(monthsToSubtract: number): YearMonth Returns a copy of this year-month with the specified period in months subtracted. |
|
| public |
minusYears(yearsToSubtract: number): YearMonth Returns a copy of this year-month with the specified period in years subtracted. |
|
| public |
Gets the month-of-year field using the {@code Month} enum. |
|
| public |
monthValue(): number Gets the month-of-year field from 1 to 12. |
|
| public |
plus(amount: TemporalAmount | number, unit: TemporalUnit): YearMonth function overloading for YearMonth.plus |
|
| public |
plusAmount(amount: TemporalAmount): YearMonth Returns a copy of this year-month with the specified period added. |
|
| public |
plusAmountUnit(amountToAdd: number, unit: TemporalUnit): YearMonth |
|
| public |
plusMonths(monthsToAdd: number): YearMonth Returns a copy of this year-month with the specified period in months added. |
|
| public |
Returns a copy of this year-month with the specified period in years added. |
|
| public |
query(query: TemporalQuery): * Queries this year-month using the specified query. |
|
| public |
range(field: TemporalField): ValueRange Gets the range of valid values for the specified field. |
|
| public |
Outputs this year-month as a {@code String}, such as {@code 2007-12}. |
|
| public |
until(endExclusive: Temporal, unit: TemporalUnit): number Calculates the period between this year-month and another year-month in terms of the specified unit. |
|
| public |
with(arg1: TemporalField | TemporalAdjuster, arg2: number | null): YearMonth function overloading for YearMonth.of |
|
| public |
withAdjuster(adjuster: TemporalAdjuster): YearMonth Returns an adjusted copy of this year-month. |
|
| public |
withFieldValue(field: TemporalField, newValue: number): * Returns a copy of this year-month with the specified field set to a new value. |
|
| public |
Returns a copy of this {@code YearMonth} with the month-of-year altered. |
|
| public |
Returns a copy of this {@code YearMonth} with the year altered. |
|
| public |
withYearMonth(newYear: number, newMonth: number): * Returns a copy of this year-month with the new year and month, checking to see if a new object is in fact required. |
|
| public |
Gets the year field. |
|
Inherited Summary
| From class TemporalAccessor | ||
| public |
get(field: TemporalField): number Gets the value of the specified field as an {@code int}. |
|
| public |
query(query: TemporalQuery): * Queries this date-time. |
|
| public |
range(field: TemporalField): ValueRange Gets the range of valid values for the specified field. |
|
Static Public Methods
public static from(temporal: TemporalAccessor): YearMonth source
Obtains an instance of {@code YearMonth} from a temporal object.
A {@code TemporalAccessor} represents some form of date and time information. This factory converts the arbitrary temporal object to an instance of {@code YearMonth}.
The conversion extracts the {@link ChronoField#YEAR YEAR} and {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} fields. The extraction is only permitted if the temporal object has an ISO chronology, or can be converted to a {@code LocalDate}.
This method matches the signature of the functional interface TemporalQuery allowing it to be used in queries via method reference, {@code YearMonth::from}.
Params:
| Name | Type | Attribute | Description |
| temporal | TemporalAccessor | the temporal object to convert, not null |
Throw:
* |
DateTimeException if unable to convert to a {@code YearMonth} |
public static now(arg1: ZoneId | Clock | null): YearMonth source
function overloading for YearMonth.now
if called with 0 argument YearMonth.now0 is applied,
if called with 1 argument and first argument is an instance of ZoneId, then YearMonth.nowZoneId is applied,
otherwise YearMonth.nowClock is applied
public static now0(): YearMonth source
Obtains the current year-month from the system clock in the default time-zone.
This will query the {@link Clock#systemDefaultZone() system clock} in the default time-zone to obtain the current year-month. The zone and offset will be set based on the time-zone in the clock.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
public static nowClock(clock: Clock): YearMonth source
Obtains the current year-month from the specified clock.
This will query the specified clock to obtain the current year-month. Using this method allows the use of an alternate clock for testing. The alternate clock may be introduced using {@link Clock dependency injection}.
Params:
| Name | Type | Attribute | Description |
| clock | Clock | the clock to use, not null |
public static nowZoneId(zone: ZoneId): YearMonth source
Obtains the current year-month from the system clock in the specified time-zone.
This will query the {@link Clock#system(ZoneId) system clock} to obtain the current year-month. Specifying the time-zone avoids dependence on the default time-zone.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
Params:
| Name | Type | Attribute | Description |
| zone | ZoneId | the zone ID to use, not null |
public static of(arg1: Month | number, arg2: number | null): YearMonth source
function overloading for YearMonth.of
if called with 2 argument and first argument is an instance of Month, then YearMonth.ofNumberMonth is applied,
otherwise YearMonth.ofNumberNumber is applied
public static ofNumberMonth(year: number, month: Month): YearMonth source
Obtains an instance of {@code YearMonth} from a year and month.
Throw:
* |
DateTimeException if the year value is invalid |
public static ofNumberNumber(year: number, month: number): YearMonth source
Obtains an instance of {@code YearMonth} from a year and month.
Throw:
* |
DateTimeException if either field value is invalid |
public static parse(arg1: String, arg2: DateTimeFormatter | null): MonthDay source
function overloading for YearMonth.parse
if called with 2 argument and first argument is an instance of Month, then YearMonth.parseString is applied,
otherwise YearMonth.parseStringFormatter is applied
Params:
| Name | Type | Attribute | Description |
| arg1 | String |
|
|
| arg2 | DateTimeFormatter | null |
|
public static parseString(text: String): YearMonth source
Obtains an instance of {@code YearMonth} from a text string such as {@code 2007-12}.
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.
Params:
| Name | Type | Attribute | Description |
| text | String | the text to parse such as "2007-12", not null |
Throw:
* |
DateTimeParseException if the text cannot be parsed |
public static parseStringFormatter(text: String, formatter: DateTimeFormatter): * source
Obtains an instance of {@code YearMonth} from a text string using a specific formatter.
The text is parsed using the formatter, returning a year-month.
Params:
| Name | Type | Attribute | Description |
| text | String | the text to parse, not null |
|
| formatter | DateTimeFormatter | the formatter to use, not null |
Return:
| * | the parsed year-month, not null |
Throw:
* |
DateTimeParseException if the text cannot be parsed |
Public Constructors
Public Methods
public adjustInto(temporal: Temporal): Temporal source
Adjusts the specified temporal object to have this year-month.
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.
The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} passing ChronoField#PROLEPTIC_MONTH as the field. If the specified temporal object does not use the ISO calendar system then a {@code DateTimeException} is thrown.
In most cases, it is clearer to reverse the calling pattern by using {@link Temporal#with(TemporalAdjuster)}:
// these two lines are equivalent, but the second approach is recommended temporal = thisYearMonth.adjustInto(temporal); temporal = temporal.with(thisYearMonth);
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| temporal | Temporal | the target object to be adjusted, not null |
Throw:
* |
DateTimeException if unable to make the adjustment |
* |
ArithmeticException if numeric overflow occurs |
public atDay(dayOfMonth: number): LocalDate source
Combines this year-month with a day-of-month to create a {@code LocalDate}.
This returns a {@code LocalDate} formed from this year-month and the specified day-of-month.
The day-of-month value must be valid for the year-month.
This method can be used as part of a chain to produce a date:
LocalDate date = year.atMonth(month).atDay(day);
Params:
| Name | Type | Attribute | Description |
| dayOfMonth | number | the day-of-month to use, from 1 to 31 |
Throw:
* |
DateTimeException if the day is invalid for the year-month |
See:
public atEndOfMonth(): LocalDate source
Returns a {@code LocalDate} at the end of the month.
This returns a {@code LocalDate} based on this year-month. The day-of-month is set to the last valid day of the month, taking into account leap years.
This method can be used as part of a chain to produce a date:
LocalDate date = year.atMonth(month).atEndOfMonth();
public compareTo(other: YearMonth): number source
Compares this year-month to another year-month.
The comparison is based first on the value of the year, then on the value of the month. It is "consistent with equals", as defined by Comparable.
Params:
| Name | Type | Attribute | Description |
| other | YearMonth | the other year-month to compare to, not null |
public equals(obj: *): boolean source
Checks if this year-month is equal to another year-month.
The comparison is based on the time-line position of the year-months.
Params:
| Name | Type | Attribute | Description |
| obj | * | the object to check, null returns false |
public format(formatter: DateTimeFormatter): String source
Outputs this year-month as a {@code String} using the formatter.
This year-month will be passed to the formatter {@link DateTimeFormatter#format(TemporalAccessor) print method}.
Params:
| Name | Type | Attribute | Description |
| formatter | DateTimeFormatter | the formatter to use, not null |
Throw:
* |
DateTimeException if an error occurs during printing |
public get(field: TemporalField): number source
Gets the value of the specified field from this year-month as an {@code int}.
This queries this year-month for the value for the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a 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}.
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.
Override:
TemporalAccessor#getParams:
| Name | Type | Attribute | Description |
| field | TemporalField | the field to get, not null |
Throw:
* |
DateTimeException if a value for the field cannot be obtained |
* |
ArithmeticException if numeric overflow occurs |
public getLong(field: TemporalField): number source
Gets the value of the specified field from this year-month as a {@code long}.
This queries this year-month for the value for the specified field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a 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}.
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.
Params:
| Name | Type | Attribute | Description |
| field | TemporalField | the field to get, not null |
Throw:
* |
DateTimeException if a value for the field cannot be obtained |
* |
ArithmeticException if numeric overflow occurs |
public isAfter(other: YearMonth): boolean source
Is this year-month after the specified year-month.
Params:
| Name | Type | Attribute | Description |
| other | YearMonth | the other year-month to compare to, not null |
public isBefore(other: YearMonth): boolean source
Is this year-month before the specified year-month.
Params:
| Name | Type | Attribute | Description |
| other | YearMonth | the other year-month to compare to, not null |
public isLeapYear(): boolean source
Checks if the year is a leap year, according to the ISO proleptic calendar system rules.
This method applies the current rules for leap years across the whole time-line. In general, a year is a leap year if it is divisible by four without remainder. However, years divisible by 100, are not leap years, with the exception of years divisible by 400 which are.
For example, 1904 is a leap year it is divisible by 4. 1900 was not a leap year as it is divisible by 100, however 2000 was a leap year as it is divisible by 400.
The calculation is proleptic - applying the same rules into the far future and far past. This is historically inaccurate, but is correct for the ISO-8601 standard.
public isSupported(arg1: TemporalField | ChronoUnit): boolean source
function overloading for YearMonth.isSupported
if called with 1 argument and first argument is an instance of TemporalField, then YearMonth.isSupportedField is applied,
otherwise YearMonth.isSupportedUnit is applied
Params:
| Name | Type | Attribute | Description |
| arg1 | TemporalField | ChronoUnit |
|
public isSupportedField(field: TemporalField): boolean source
Checks if the specified field is supported.
This checks if this year-month can be queried for the specified field. If false, then calling the {@link #range(TemporalField) range} and {@link #get(TemporalField) get} methods will throw an exception.
If the field is a ChronoField then the query is implemented here. The {@link #isSupported(TemporalField) supported fields} will return valid values based on this date-time. The supported fields are:
- {@code MONTH_OF_YEAR}
- {@code EPOCH_MONTH}
- {@code YEAR_OF_ERA}
- {@code YEAR}
- {@code ERA}
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.
Params:
| Name | Type | Attribute | Description |
| field | TemporalField | the field to check, null returns false |
public isValidDay(dayOfMonth: number): boolean source
Checks if the day-of-month is valid for this year-month.
This method checks whether this year and month and the input day form a valid date.
Params:
| Name | Type | Attribute | Description |
| dayOfMonth | number | the day-of-month to validate, from 1 to 31, invalid value returns false |
public lengthOfMonth(): number source
Returns the length of the month, taking account of the year.
This returns the length of the month in days. For example, a date in January would return 31.
public lengthOfYear(): number source
Returns the length of the year.
This returns the length of the year in days, either 365 or 366.
public minus(amount: TemporalAmount | number, unit: TemporalUnit): YearMonth source
function overloading for YearMonth.minus
if called with 1 arguments, then YearMonth.minusAmount is executed.
Otherwise YearMonth.minusAmountUnit is executed.
Params:
| Name | Type | Attribute | Description |
| amount | TemporalAmount | number |
|
|
| unit | TemporalUnit |
public minusAmount(amount: TemporalAmount): YearMonth source
Returns a copy of this year-month with the specified period subtracted.
This method returns a new year-month based on this year-month with the specified period subtracted. The subtractor is typically {@link org.threeten.bp.Period Period} but may be any other type implementing the TemporalAmount interface. The calculation is delegated to the specified adjuster, which typically calls back to {@link #minus(long, TemporalUnit)}.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| amount | TemporalAmount | the amount to subtract, not null |
Throw:
* |
DateTimeException if the subtraction cannot be made |
* |
ArithmeticException if numeric overflow occurs |
public minusAmountUnit(amountToSubtract: number, unit: TemporalUnit): YearMonth source
Params:
| Name | Type | Attribute | Description |
| amountToSubtract | number | the amount to subtract, not null |
|
| unit | TemporalUnit |
Throw:
* |
DateTimeException {@inheritDoc} |
* |
ArithmeticException {@inheritDoc} |
public minusMonths(monthsToSubtract: number): YearMonth source
Returns a copy of this year-month with the specified period in months subtracted.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| monthsToSubtract | number | the months to subtract, may be negative |
Throw:
* |
DateTimeException if the result exceeds the supported range |
public minusYears(yearsToSubtract: number): YearMonth source
Returns a copy of this year-month with the specified period in years subtracted.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| yearsToSubtract | number | the years to subtract, may be negative |
Throw:
* |
DateTimeException if the result exceeds the supported range |
public month(): Month source
Gets the month-of-year field using the {@code Month} enum.
This method returns the enum Month for the month. This avoids confusion as to what {@code int} values mean. If you need access to the primitive {@code int} value then the enum provides the {@link Month#getValue() int value}.
public monthValue(): number source
Gets the month-of-year field from 1 to 12.
This method returns the month as an {@code int} from 1 to 12. Application code is frequently clearer if the enum Month is used by calling {@link #getMonth()}.
See:
public plus(amount: TemporalAmount | number, unit: TemporalUnit): YearMonth source
function overloading for YearMonth.plus
if called with 1 arguments, then YearMonth.plusAmount is executed.
Otherwise YearMonth.plusAmountUnit is executed.
Params:
| Name | Type | Attribute | Description |
| amount | TemporalAmount | number |
|
|
| unit | TemporalUnit |
public plusAmount(amount: TemporalAmount): YearMonth source
Returns a copy of this year-month with the specified period added.
This method returns a new year-month based on this year-month with the specified period added. The adder is typically {@link org.threeten.bp.Period Period} but may be any other type implementing the TemporalAmount interface. The calculation is delegated to the specified adjuster, which typically calls back to {@link #plus(long, TemporalUnit)}.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| amount | TemporalAmount | the amount to add, not null |
Throw:
* |
DateTimeException if the addition cannot be made |
* |
ArithmeticException if numeric overflow occurs |
public plusAmountUnit(amountToAdd: number, unit: TemporalUnit): YearMonth source
Params:
| Name | Type | Attribute | Description |
| amountToAdd | number | ||
| unit | TemporalUnit |
Throw:
* |
DateTimeException {@inheritDoc} |
* |
ArithmeticException {@inheritDoc} |
public plusMonths(monthsToAdd: number): YearMonth source
Returns a copy of this year-month with the specified period in months added.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| monthsToAdd | number | the months to add, may be negative |
Throw:
* |
DateTimeException if the result exceeds the supported range |
public plusYears(yearsToAdd: number): YearMonth source
Returns a copy of this year-month with the specified period in years added.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| yearsToAdd | number | the years to add, may be negative |
Throw:
* |
DateTimeException if the result exceeds the supported range |
public query(query: TemporalQuery): * source
Queries this year-month using the specified query.
This queries this year-month using the specified query strategy object. The {@code TemporalQuery} object defines the logic to be used to obtain the result. Read the documentation of the query to understand what the result of this method will be.
The result of this method is obtained by invoking the {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the specified query passing {@code this} as the argument.
Override:
TemporalAccessor#queryParams:
| Name | Type | Attribute | Description |
| query | TemporalQuery | the query to invoke, not null |
Return:
| * | the query result, null may be returned (defined by the query) |
Throw:
* |
DateTimeException if unable to query (defined by the query) |
* |
ArithmeticException if numeric overflow occurs (defined by the query) |
public range(field: TemporalField): ValueRange source
Gets the range of valid values for the specified field.
The range object expresses the minimum and maximum valid values for a field. This year-month is used to enhance the accuracy of the returned range. If it is not possible to return the range, because the field is not supported or for some other reason, an exception is thrown.
If the field is a 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}.
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.
Override:
TemporalAccessor#rangeParams:
| Name | Type | Attribute | Description |
| field | TemporalField | the field to query the range for, not null |
Throw:
* |
DateTimeException if the range for the field cannot be obtained |
public toString(): String source
Outputs this year-month as a {@code String}, such as {@code 2007-12}.
The output will be in the format {@code yyyy-MM}:
public until(endExclusive: Temporal, unit: TemporalUnit): number source
Calculates the period between this year-month and another year-month in terms of the specified unit.
This calculates the period between two year-months in terms of a single unit. The start and end points are {@code this} and the specified year-month. The result will be negative if the end is before the start. The {@code Temporal} passed to this method must be a {@code YearMonth}. For example, the period in years between two year-months can be calculated using {@code startYearMonth.until(endYearMonth, YEARS)}.
The calculation returns a whole number, representing the number of complete units between the two year-months. For example, the period in decades between 2012-06 and 2032-05 will only be one decade as it is one month short of two decades.
This method operates in association with TemporalUnit#between. The result of this method is a {@code long} representing the amount of the specified unit. By contrast, the result of {@code between} is an object that can be used directly in addition/subtraction:
long period = start.until(end, YEARS); // this method dateTime.plus(YEARS.between(start, end)); // use in plus/minus
The calculation is implemented in this method for ChronoUnit. The units {@code MONTHS}, {@code YEARS}, {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported. Other {@code ChronoUnit} values will throw an exception.
If the unit is not a {@code ChronoUnit}, then the result of this method is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} passing {@code this} as the first argument and the input temporal as the second argument.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| endExclusive | Temporal | the end year-month, which is converted to a {@code YearMonth}, not null |
|
| unit | TemporalUnit | the unit to measure the period in, not null |
Throw:
* |
DateTimeException if the period cannot be calculated |
* |
ArithmeticException if numeric overflow occurs |
public with(arg1: TemporalField | TemporalAdjuster, arg2: number | null): YearMonth source
function overloading for YearMonth.of
if called with 1 argument, then YearMonth.withAdjuster is applied,
if called with 2 arguments and first argument is an instance of TemporalField, then YearMonth.withFieldValue is applied,
otherwise YearMonth.withYearMonth is applied
Params:
| Name | Type | Attribute | Description |
| arg1 | TemporalField | TemporalAdjuster |
|
|
| arg2 | number | null |
|
public withAdjuster(adjuster: TemporalAdjuster): YearMonth source
Returns an adjusted copy of this year-month.
This returns a new {@code YearMonth}, based on this one, with the year-month adjusted. The adjustment takes place using the specified adjuster strategy object. Read the documentation of the adjuster to understand what adjustment will be made.
A simple adjuster might simply set the one of the fields, such as the year field. A more complex adjuster might set the year-month to the next month that Halley's comet will pass the Earth.
The result of this method is obtained by invoking the {@link TemporalAdjuster#adjustInto(Temporal)} method on the specified adjuster passing {@code this} as the argument.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| adjuster | TemporalAdjuster | the adjuster to use, not null |
Throw:
* |
DateTimeException if the adjustment cannot be made |
* |
ArithmeticException if numeric overflow occurs |
public withFieldValue(field: TemporalField, newValue: number): * source
Returns a copy of this year-month with the specified field set to a new value.
This returns a new {@code YearMonth}, based on this one, with the value for the specified field changed. This can be used to change any supported field, such as the year or month. If it is not possible to set the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a ChronoField then the adjustment is implemented here. The supported fields behave as follows:
- {@code MONTH_OF_YEAR} - Returns a {@code YearMonth} with the specified month-of-year. The year will be unchanged.
- {@code PROLEPTIC_MONTH} - Returns a {@code YearMonth} with the specified proleptic-month. This completely replaces the year and month of this object.
- {@code YEAR_OF_ERA} - Returns a {@code YearMonth} with the specified year-of-era The month and era will be unchanged.
- {@code YEAR} - Returns a {@code YearMonth} with the specified year. The month will be unchanged.
- {@code ERA} - Returns a {@code YearMonth} with the specified era. The month and year-of-era will be unchanged.
In all cases, if the new value is outside the valid range of values for the field then a {@code DateTimeException} will be thrown.
All other {@code ChronoField} instances will throw a {@code DateTimeException}.
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.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| field | TemporalField | the field to set in the result, not null |
|
| newValue | number | the new value of the field in the result |
Return:
| * | a {@code YearMonth} based on {@code this} with the specified field set, not null |
Throw:
* |
DateTimeException if the field cannot be set |
* |
ArithmeticException if numeric overflow occurs |
public withMonth(month: number): YearMonth source
Returns a copy of this {@code YearMonth} with the month-of-year altered.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| month | number | the month-of-year to set in the returned year-month, from 1 (January) to 12 (December) |
Throw:
* |
DateTimeException if the month-of-year value is invalid |
public withYear(year: number): YearMonth source
Returns a copy of this {@code YearMonth} with the year altered.
This instance is immutable and unaffected by this method call.
Params:
| Name | Type | Attribute | Description |
| year | number | the year to set in the returned year-month, from MIN_YEAR to MAX_YEAR |
Throw:
* |
DateTimeException if the year value is invalid |