Home Reference Source
public class | source

DayOfMonth

Extends:

@js-joda/core~TemporalAccessor → DayOfMonth

A day-of-month in the ISO-8601 calendar system.

DayOfMonth is an immutable date-time object that represents a day-of-month. It is a type-safe way of representing a day-of-month in an application. Any field that can be derived from a day-of-month can be obtained.

This class does not store or represent a year, month, time or time-zone. For example, the value '21' can be stored in a DayOfMonth and would represent the 21st day of any month.

Static Method Summary

Static Public Methods
public static

Obtains an instance of DayOfMonth from a date-time object.

public static

now(zoneIdOrClock: ZoneId | Clock): DayOfMonth

Function overloading for DayOfMonth.now:

public static

of(dayOfMonth: number): DayOfMonth

Obtains an instance of DayOfMonth.

Static Protected Methods
protected static

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

protected static

Obtains the current day-of-month from the specified clock.

protected static

Obtains the current day-of-month from the system clock in the specified time-zone.

Method Summary

Public Methods
public

Adjusts the specified temporal object to have this day-of-month.

public

atMonth(month: Month | number): MonthDay

Combines this day-of-month with a month to create a MonthDay.

public

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

public

compareTo(other: DayOfMonth): number

Compares this day-of-month to another.

public

equals(obj: *): boolean

Checks if this day-of-month is equal to another day-of-month.

public

get(field: TemporalField): number

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

public

getLong(field: TemporalField): number

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

public

hashCode(): number

A hash code for this day-of-month.

public

isSupported(field: TemporalField): boolean

Checks if the specified field is supported.

public

isValidYearMonth(yearMonth: YearMonth): boolean

Checks if the year-month is valid for this year.

public

query(query: TemporalQuery): *

Queries this day-of-month using the specified query.

public

Gets the range of valid values for the specified field.

public

toString(): string

Outputs this day-of-month as a String.

public

value(): number

Gets the day-of-month value.

Static Public Methods

public static from(temporal: TemporalAccessor): DayOfMonth source

Obtains an instance of DayOfMonth from a date-time object.

This obtains a day-of-month based on the specified temporal. A TemporalAccessor represents an arbitrary set of date and time information, which this factory converts to an instance of DayOfMonth.

The conversion extracts the ChronoField#DAY_OF_MONTH day-of-month field. 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, DayOfMonth.from.

Params:

NameTypeAttributeDescription
temporal TemporalAccessor

the temporal object to convert, not null

Return:

DayOfMonth

the day-of-month, not null

Throw:

DateTimeException

if unable to convert to a DayOfMonth

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

Function overloading for DayOfMonth.now:

Params:

NameTypeAttributeDescription
zoneIdOrClock ZoneId | Clock
  • nullable: true

Return:

DayOfMonth

public static of(dayOfMonth: number): DayOfMonth source

Obtains an instance of DayOfMonth.

A day-of-month object represents one of the 31 days of the month, from 1 to 31.

Params:

NameTypeAttributeDescription
dayOfMonth number

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

Return:

DayOfMonth

the day-of-month, not null

Throw:

DateTimeException

if the day-of-month is invalid

Static Protected Methods

protected static _now0(): DayOfMonth source

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

This will query the Clock.systemDefaultZone system clock in the default time-zone to obtain the current day-of-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:

DayOfMonth

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

protected static _nowClock(clock: Clock): DayOfMonth source

Obtains the current day-of-month from the specified clock.

This will query the specified clock to obtain the current day-of-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:

NameTypeAttributeDescription
clock Clock

the clock to use, not null

Return:

DayOfMonth

the current day-of-month, not null

protected static _nowZoneId(zone: ZoneId): DayOfMonth source

Obtains the current day-of-month from the system clock in the specified time-zone.

This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current day-of-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:

DayOfMonth

the current day-of-month using the system clock, not null

Public Methods

public adjustInto(temporal: Temporal): Temporal source

Adjusts the specified temporal object to have this day-of-month.

This returns a temporal object of the same observable type as the input with the day-of-month changed to be the same as this.

The adjustment is equivalent to using Temporal.with passing ChronoField.DAY_OF_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 = thisDay.adjustInto(temporal);
  temporal = temporal.with(thisDay);

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 atMonth(month: Month | number): MonthDay source

Combines this day-of-month with a month to create a MonthDay.

This returns a MonthDay formed from this day and the specified month.

This method can be used as part of a chain to produce a date:

 LocalDate date = day.atMonth(month).atYear(year);

If this day-of-month is invalid for the month then it will be changed to the last valid date for the month.

Params:

NameTypeAttributeDescription
month Month | number

the month-of-year to use, from 1 (January) to 12 (December), not null

Return:

MonthDay

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

public atYearMonth(yearMonth: YearMonth): LocalDate source

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

This returns a LocalDate formed from this year and the specified year-month.

If this day-of-month is invalid for the year-month then it will be changed to the last valid date for the month.

Params:

NameTypeAttributeDescription
yearMonth YearMonth

the year-month to use, not null

Return:

LocalDate

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

public compareTo(other: DayOfMonth): number source

Compares this day-of-month to another.

The comparison is based on the value of the day. It is 'consistent with equals', as defined by Comparable.

Params:

NameTypeAttributeDescription
other DayOfMonth

the other day-of-month instance, not null

Return:

number

the comparator value, negative if less, positive if greater

public equals(obj: *): boolean source

Checks if this day-of-month is equal to another day-of-month.

Params:

NameTypeAttributeDescription
obj *

the other day-of-month instance, null returns false

Return:

boolean

true if the day-of-month is the same

public get(field: TemporalField): number source

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

This queries this day-of-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 DayOfMonth.isSupported supported fields will return valid values based on this day-of-month. All other ChronoField instances will throw an UnsupportedTemporalTypeException.

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.

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 or the value is outside the range of valid values for the field

UnsupportedTemporalTypeException

if the field is not supported or the range of values exceeds an int

ArithmeticException

if numeric overflow occurs

public getLong(field: TemporalField): number source

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

This queries this day-of-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 DayOfMonth.isSupported(TemporalField) supported fields} will return valid values based on this day-of-month. All other ChronoField instances will throw an UnsupportedTemporalTypeException.

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.

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

UnsupportedTemporalTypeException

if the field is not supported

ArithmeticException

if numeric overflow occurs

public hashCode(): number source

A hash code for this day-of-month.

Return:

number

a suitable hash code

public isSupported(field: TemporalField): boolean source

Checks if the specified field is supported.

This checks if this day-of-month can be queried for the specified field. If false, then calling the DayOfMonth.range range, DayOfMonth.get get and DayOfMonth.getLong getLong methods will throw an exception.

If the field is a ChronoField then the query is implemented here. The supported fields are:

  • ChronoField.DAY_OF_MONTH 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 day-of-month, false if not

    public isValidYearMonth(yearMonth: YearMonth): boolean source

    Checks if the year-month is valid for this year.

    This method checks whether this day and the input year and month form a valid date.

    Params:

    NameTypeAttributeDescription
    yearMonth YearMonth

    the year month to validate, null returns false

    Return:

    boolean

    true if the year and month are valid for this day

    public query(query: TemporalQuery): * source

    Queries this day-of-month using the specified query.

    This queries this day-of-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.

    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 day-of-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 DayOfMonth.isSupported supported fields will return appropriate range instances. All other ChronoField instances will throw an UnsupportedTemporalTypeException.

    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.

    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

    UnsupportedTemporalTypeException

    if the field is not supported

    public toString(): string source

    Outputs this day-of-month as a String.

    Return:

    string

    a string representation of this day-of-month, not null

    public value(): number source

    Gets the day-of-month value.

    Return:

    number

    the day-of-month, from 1 to 31