Home Reference Source
public class | source

YearMonth

Extends:

TemporalAccessorTemporal → YearMonth

A year-month in the ISO-8601 calendar system, such as 2007-12.

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 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

Obtains an instance of YearMonth from a temporal object.

public static

now(zoneIdOrClock: ZoneId | Clock): YearMonth

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

of(year: number, monthOrNumber: Month | number): YearMonth

function overloading for YearMonth.of

public static

ofNumberMonth(year: number, month: Month): YearMonth

Obtains an instance of YearMonth from a year and month.

public static

ofNumberNumber(year: number, month: number): YearMonth

Obtains an instance of YearMonth from a year and month.

public static

parse(text: String, formatter: DateTimeFormatter): YearMonth

function overloading for YearMonth.parse

public static

parseString(text: String): YearMonth

Obtains an instance of YearMonth from a text string such as 2007-12.

public static

parseStringFormatter(text: String, formatter: DateTimeFormatter): *

Obtains an instance of YearMonth from a text string using a specific formatter.

Method Summary

Public Methods
public

Adjusts the specified temporal object to have this year-month.

public

atDay(dayOfMonth: number): LocalDate

Combines this year-month with a day-of-month to create a LocalDate.

public

Returns a LocalDate at the end of the month.

public

compareTo(other: YearMonth): number

Compares this year-month to another year-month.

public

equals(obj: *): boolean

Checks if this year-month is equal to another year-month.

public

format(formatter: DateTimeFormatter): String

Outputs this year-month as a string using the formatter.

public

get(field: TemporalField): number

Gets the value of the specified field from this year-month as an int.

public

getLong(field: TemporalField): number

Gets the value of the specified field from this year-month as a long.

public

isAfter(other: YearMonth): boolean

Is this year-month after the specified year-month.

public

isBefore(other: YearMonth): boolean

Is this year-month before the specified year-month.

public

isLeapYear(): boolean

Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

public

isSupported(fieldOrUnit: 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

lengthOfMonth(): number

Returns the length of the month, taking account of the year.

public

lengthOfYear(): number

Returns the length of the year.

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 Month enum.

public

monthValue(): number

Gets the month-of-year field from 1 to 12.

public

plusMonths(monthsToAdd: number): YearMonth

Returns a copy of this year-month with the specified period in months added.

public

plusYears(yearsToAdd: number): YearMonth

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

Gets the range of valid values for the specified field.

public

toJSON(): string

toJSON() use by JSON.stringify delegates to toString()

public

toString(): String

Outputs this year-month as a string, such as 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(adjusterOrField: TemporalAdjuster | TemporalField, value: number): YearMonth

function overloading for YearMonth.with

public

withMonth(month: number): YearMonth

Returns a copy of this YearMonth with the month-of-year altered.

public

withYear(year: number): YearMonth

Returns a copy of this YearMonth with the year altered.

public

year(): number

Gets the year field.

Inherited Summary

From class TemporalAccessor
public

get(field: TemporalField): number

Gets the value of the specified field as an int.

public

getLong(field: *)

public

isSupported(field: *)

public

query(query: TemporalQuery): *

Queries this date-time.

public

Gets the range of valid values for the specified field.

From class Temporal
public

isSupported(fieldOrUnit: TemporalUnit): boolean

Checks if the specified unit is supported.

public

minus(amount: TemporalAmount | number, unit: TemporalUnit): Temporal

function overloading for Temporal.plus

public

plus(amount: TemporalAmount | number, unit: TemporalUnit): Temporal

function overloading for Temporal.plus

public

until(endTemporal: Temporal, unit: TemporalUnit): number

Calculates the period between this temporal and another temporal in terms of the specified unit.

public

with(adjusterOrField: TemporalAdjuster | TemporalField, newValue: number): Temporal

function overloading for Temporal.with

Static Public Methods

public static from(temporal: TemporalAccessor): YearMonth source

Obtains an instance of YearMonth from a temporal object.

A TemporalAccessor represents some form of date and time information. This factory converts the arbitrary temporal object to an instance of YearMonth.

The conversion extracts the ChronoField#YEAR and ChronoField#MONTH_OF_YEAR fields. The extraction is only permitted if the temporal object has an ISO chronology, or can be converted to a LocalDate.

This method matches the signature of the functional interface TemporalQuery allowing it to be used in queries via method reference, YearMonth::from.

Params:

NameTypeAttributeDescription
temporal TemporalAccessor

the temporal object to convert, not null

Return:

YearMonth

the year-month, not null

Throw:

*

DateTimeException if unable to convert to a YearMonth

public static now(zoneIdOrClock: ZoneId | Clock): YearMonth source

function overloading for YearMonth.now

if called with 0 argument YearMonth.now0 is executed,

if called with 1 argument and first argument is an instance of ZoneId, then YearMonth.nowZoneId is executed,

otherwise YearMonth.nowClock is executed

Params:

NameTypeAttributeDescription
zoneIdOrClock ZoneId | Clock
  • nullable: true

Return:

YearMonth

public static now0(): YearMonth source

Obtains the current year-month from the system clock in the default time-zone.

This will query the system clock (see Clock#systemDefaultZone) 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.

Return:

YearMonth

the current year-month using the system clock and default time-zone, not null

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 dependency injection.

Params:

NameTypeAttributeDescription
clock Clock

the clock to use, not null

Return:

YearMonth

the current year-month, 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 system clock (see Clock#system) 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:

NameTypeAttributeDescription
zone ZoneId

the zone ID to use, not null

Return:

YearMonth

the current year-month using the system clock, not null

public static of(year: number, monthOrNumber: Month | number): YearMonth source

function overloading for YearMonth.of

if called with 2 argument and first argument is an instance of Month, then YearMonth.ofNumberMonth is executed,

otherwise YearMonth.ofNumberNumber is executed

Params:

NameTypeAttributeDescription
year number
  • nullable: false
monthOrNumber Month | number
  • nullable: false

Return:

YearMonth

public static ofNumberMonth(year: number, month: Month): YearMonth source

Obtains an instance of YearMonth from a year and month.

Params:

NameTypeAttributeDescription
year number

the year to represent, from MIN_YEAR to MAX_YEAR

month Month

the month-of-year to represent, not null

Return:

YearMonth

the year-month, not null

Throw:

*

DateTimeException if the year value is invalid

public static ofNumberNumber(year: number, month: number): YearMonth source

Obtains an instance of YearMonth from a year and month.

Params:

NameTypeAttributeDescription
year number

the year to represent, from MIN_YEAR to MAX_YEAR

month number

the month-of-year to represent, from 1 (January) to 12 (December)

Return:

YearMonth

the year-month, not null

Throw:

*

DateTimeException if either field value is invalid

public static parse(text: String, formatter: DateTimeFormatter): YearMonth source

function overloading for YearMonth.parse

if called with 2 argument and first argument is an instance of Month, then YearMonth.parseString is executed,

otherwise YearMonth.parseStringFormatter is executed

Params:

NameTypeAttributeDescription
text String
  • nullable: false
formatter DateTimeFormatter
  • nullable: true

Return:

YearMonth

public static parseString(text: String): YearMonth source

Obtains an instance of YearMonth from a text string such as 2007-12.

The string must represent a valid year-month. The format must be yyyy-MM. Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.

Params:

NameTypeAttributeDescription
text String

the text to parse such as "2007-12", not null

Return:

YearMonth

the parsed year-month, not null

Throw:

*

DateTimeParseException if the text cannot be parsed

public static parseStringFormatter(text: String, formatter: DateTimeFormatter): * source

Obtains an instance of YearMonth from a text string using a specific formatter.

The text is parsed using the formatter, returning a year-month.

Params:

NameTypeAttributeDescription
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 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 Temporal#with passing ChronoField#PROLEPTIC_MONTH as the field. If the specified temporal object does not use the ISO calendar system then a DateTimeException is thrown.

In most cases, it is clearer to reverse the calling pattern by using Temporal#with:

  // 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:

NameTypeAttributeDescription
temporal Temporal

the target object to be adjusted, not null

Return:

Temporal

the adjusted object, 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 LocalDate.

This returns a 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:

NameTypeAttributeDescription
dayOfMonth number

the day-of-month to use, from 1 to 31

Return:

LocalDate

the date formed from this year-month and the specified day, not null

Throw:

*

DateTimeException if the day is invalid for the year-month

See:

public atEndOfMonth(): LocalDate source

Returns a LocalDate at the end of the month.

This returns a 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();

Return:

LocalDate

the last valid date of this year-month, not null

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:

NameTypeAttributeDescription
other YearMonth

the other year-month to compare to, not null

Return:

number

the comparator value, negative if less, positive if greater

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:

NameTypeAttributeDescription
obj *

the object to check, null returns false

Return:

boolean

true if this is equal to the other year-month

public format(formatter: DateTimeFormatter): String source

Outputs this year-month as a string using the formatter.

Params:

NameTypeAttributeDescription
formatter DateTimeFormatter

the formatter to use, not null

Return:

String

the formatted year-month string, 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 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 supported fields (see isSupported) will return valid values based on this year-month, except EPOCH_MONTH which is too large to fit in an int and throw a DateTimeException. All other ChronoField instances will throw a DateTimeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.

Override:

TemporalAccessor#get

Params:

NameTypeAttributeDescription
field TemporalField

the field to get, not null

Return:

number

the value for the field

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 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 supported fields (see isSupported) will return valid values based on this year-month. All other ChronoField instances will throw a DateTimeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.

Override:

TemporalAccessor#getLong

Params:

NameTypeAttributeDescription
field TemporalField

the field to get, not null

Return:

number

the value for the field

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:

NameTypeAttributeDescription
other YearMonth

the other year-month to compare to, not null

Return:

boolean

true if this is after the specified year-month

public isBefore(other: YearMonth): boolean source

Is this year-month before the specified year-month.

Params:

NameTypeAttributeDescription
other YearMonth

the other year-month to compare to, not null

Return:

boolean

true if this point is before the specified year-month

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.

Return:

boolean

true if the year is leap, false otherwise

public isSupported(fieldOrUnit: 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 executed,

otherwise YearMonth.isSupportedUnit is executed

Override:

Temporal#isSupported

Params:

NameTypeAttributeDescription
fieldOrUnit TemporalField | ChronoUnit
  • nullable: false

Return:

boolean

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 range and get will throw an exception.

If the field is a ChronoField then the query is implemented here. The supported fields (see isSupported) will return valid values based on this date-time. The supported fields are:

  • MONTH_OF_YEAR
  • EPOCH_MONTH
  • YEAR_OF_ERA
  • YEAR
  • ERA

All other ChronoField instances will return false.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.isSupportedBy passing this as the argument. Whether the field is supported is determined by the field.

Params:

NameTypeAttributeDescription
field TemporalField

the field to check, null returns false

Return:

boolean

true if the field is supported on this year-month, false if not

public isSupportedUnit(unit: *): * source

Params:

NameTypeAttributeDescription
unit *

Return:

*

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:

NameTypeAttributeDescription
dayOfMonth number

the day-of-month to validate, from 1 to 31, invalid value returns false

Return:

boolean

true if the day is valid for this year-month

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.

Return:

number

the length of the month in days, from 28 to 31

public lengthOfYear(): number source

Returns the length of the year.

This returns the length of the year in days, either 365 or 366.

Return:

number

366 if the year is leap, 365 otherwise

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:

NameTypeAttributeDescription
monthsToSubtract number

the months to subtract, may be negative

Return:

YearMonth

based on this year-month with the months subtracted, not null

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:

NameTypeAttributeDescription
yearsToSubtract number

the years to subtract, may be negative

Return:

YearMonth

based on this year-month with the years subtracted, not null

Throw:

*

DateTimeException if the result exceeds the supported range

public month(): Month source

Gets the month-of-year field using the Month enum.

This method returns the enum Month for the month. This avoids confusion as to what int values mean. If you need access to the primitive int value, use Month#getValue.

Return:

Month

the month-of-year, not null

public monthValue(): number source

Gets the month-of-year field from 1 to 12.

This method returns the month as an int from 1 to 12. Application code is frequently clearer if the enum Month is used by calling getMonth.

Return:

number

the month-of-year, from 1 to 12

See:

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:

NameTypeAttributeDescription
monthsToAdd number

the months to add, may be negative

Return:

YearMonth

based on this year-month with the months added, not null

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:

NameTypeAttributeDescription
yearsToAdd number

the years to add, may be negative

Return:

YearMonth

based on this year-month with the years added, not null

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 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 TemporalQuery#queryFrom method on the specified query passing this as the argument.

Override:

TemporalAccessor#query

Params:

NameTypeAttributeDescription
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 supported fields (see isSupported) will return appropriate range instances. All other ChronoField instances will throw a DateTimeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.rangeRefinedBy passing this as the argument. Whether the range can be obtained is determined by the field.

Override:

TemporalAccessor#range

Params:

NameTypeAttributeDescription
field TemporalField

the field to query the range for, not null

Return:

ValueRange

the range of valid values for the field, not null

Throw:

*

DateTimeException if the range for the field cannot be obtained

public toJSON(): string source

toJSON() use by JSON.stringify delegates to toString()

Return:

string

public toString(): String source

Outputs this year-month as a string, such as 2007-12.

The output will be in the format yyyy-MM:

Return:

String

a string representation of this year-month, not null

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 this and the specified year-month. The result will be negative if the end is before the start. The Temporal passed to this method must be a YearMonth. For example, the period in years between two year-months can be calculated using startYearMonth.until.

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 long representing the amount of the specified unit. By contrast, the result of 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 MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA and ERAS are supported. Other ChronoUnit values will throw an exception.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between passing this as the first argument and the input temporal as the second argument.

This instance is immutable and unaffected by this method call.

Override:

Temporal#until

Params:

NameTypeAttributeDescription
endExclusive Temporal

the end year-month, which is converted to a YearMonth, not null

unit TemporalUnit

the unit to measure the period in, not null

Return:

number

the amount of the period between this year-month and the end year-month

Throw:

*

DateTimeException if the period cannot be calculated

*

ArithmeticException if numeric overflow occurs

public with(adjusterOrField: TemporalAdjuster | TemporalField, value: number): YearMonth source

function overloading for YearMonth.with

if called with 1 argument, then YearMonth.withAdjuster is executed, otherwise YearMonth.withFieldValue is executed.

Override:

Temporal#with

Params:

NameTypeAttributeDescription
adjusterOrField TemporalAdjuster | TemporalField
  • nullable: false
value number
  • nullable: true

nullable only of first argument is an instance of TemporalAdjuster

Return:

YearMonth

public withMonth(month: number): YearMonth source

Returns a copy of this YearMonth with the month-of-year altered.

This instance is immutable and unaffected by this method call.

Params:

NameTypeAttributeDescription
month number

the month-of-year to set in the returned year-month, from 1 (January) to 12 (December)

Return:

YearMonth

based on this year-month with the requested month, not null

Throw:

*

DateTimeException if the month-of-year value is invalid

public withYear(year: number): YearMonth source

Returns a copy of this YearMonth with the year altered.

This instance is immutable and unaffected by this method call.

Params:

NameTypeAttributeDescription
year number

the year to set in the returned year-month, from MIN_YEAR to MAX_YEAR

Return:

YearMonth

based on this year-month with the requested year, not null

Throw:

*

DateTimeException if the year value is invalid

public year(): number source

Gets the year field.

This method returns the primitive int value for the year.

The year returned by this method is proleptic as per get.

Return:

number

the year, from MIN_YEAR to MAX_YEAR