Top Description Fields Constructors Methods
org.threeten.extra

public final Class DayOfMonth

extends Object
implements TemporalAccessor, TemporalAdjuster, Comparable<DayOfMonth>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable, java.time.temporal.TemporalAdjuster, java.time.temporal.TemporalAccessor
Static Imports
java.time.temporal.ChronoField.DAY_OF_MONTH

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.

Implementation Requirements:

This class is immutable and thread-safe.

This class must be treated as a value type. Do not synchronize, rely on the identity hash code or use the distinction between equals() and ==.

Field Summary

Modifier and TypeField and Description
private final int
day

The day-of-month being represented, from 1 to 31.

private static final long
serialVersionUID

Serialization version.

private static final DayOfMonth[]
VALUES

Cache of singleton instances.

Constructor Summary

AccessConstructor and Description
private
DayOfMonth(int
the day-of-month to represent
dayOfMonth
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public Temporal

Returns:

the adjusted object, not null
adjustInto
(Temporal
the target object to be adjusted, not null
temporal
)

Implements java.time.temporal.TemporalAdjuster.adjustInto.

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

public MonthDay

Returns:

the year-month formed from this year and the specified month, not null
atMonth
(Month
the month-of-year to use, not null
month
)

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

public MonthDay

Returns:

the year-month formed from this year and the specified month, not null
atMonth
(int
the month-of-year to use, from 1 (January) to 12 (December)
month
)

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

public LocalDate

Returns:

the local date formed from this year and the specified year-month, not null
atYearMonth
(YearMonth
the year-month to use, not null
yearMonth
)

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

public int

Returns:

the comparator value, negative if less, positive if greater
compareTo
(DayOfMonth
the other day-of-month instance, not null
other
)

Implements java.lang.Comparable.compareTo.

Compares this day-of-month to another.

public boolean

Returns:

true if the day-of-month is the same
equals
(Object
the other day-of-month instance, null returns false
obj
)

Overrides java.lang.Object.equals.

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

public static DayOfMonth

Returns:

the day-of-month, not null
from
(TemporalAccessor
the temporal object to convert, not null
temporal
)

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

public int

Returns:

the value for the field
get
(TemporalField
the field to get, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.get.

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

public long

Returns:

the value for the field
getLong
(TemporalField
the field to get, not null
field
)

Implements java.time.temporal.TemporalAccessor.getLong.

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

public int

Returns:

the day-of-month, from 1 to 31
getValue
()

Gets the day-of-month value.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this day-of-month.

public boolean

Returns:

true if the field is supported on this day-of-month, false if not
isSupported
(TemporalField
the field to check, null returns false
field
)

Implements java.time.temporal.TemporalAccessor.isSupported.

Checks if the specified field is supported.

public boolean

Returns:

true if the year and month are valid for this day
isValidYearMonth
(YearMonth
the year month to validate, null returns false
yearMonth
)

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

public static DayOfMonth

Returns:

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

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

public static DayOfMonth

Returns:

the current day-of-month using the system clock, not null
now
(ZoneId
the zone ID to use, not null
zone
)

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

public static DayOfMonth

Returns:

the current day-of-month, not null
now
(Clock
the clock to use, not null
clock
)

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

public static DayOfMonth

Returns:

the day-of-month, not null
of
(int
the day-of-month to represent, from 1 to 31
dayOfMonth
)

Obtains an instance of DayOfMonth.

public <
the type of the result
R
>
R

Returns:

the query result, null may be returned (defined by the query)
query
(TemporalQuery<R>
the query to invoke, not null
query
)

Overrides default java.time.temporal.TemporalAccessor.query.

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

public ValueRange

Returns:

the range of valid values for the field, not null
range
(TemporalField
the field to query the range for, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.range.

Gets the range of valid values for the specified field.

private Object

Returns:

the valid object, not null
readResolve
()

Validates the input.

public String

Returns:

a string representation of this day-of-month, not null
toString
()

Overrides java.lang.Object.toString.

Outputs this day-of-month as a String.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

dayback to summary
private final int day

The day-of-month being represented, from 1 to 31.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

VALUESback to summary
private static final DayOfMonth[] VALUES

Cache of singleton instances.

Constructor Detail

DayOfMonthback to summary
private DayOfMonth(int dayOfMonth)

Constructor.

Parameters
dayOfMonth:int

the day-of-month to represent

Method Detail

adjustIntoback to summary
public Temporal adjustInto(Temporal temporal)

Implements java.time.temporal.TemporalAdjuster.adjustInto.

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(TemporalField, long) 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(TemporalAdjuster):

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

Parameters
temporal:Temporal

the target object to be adjusted, not null

Returns:Temporal

the adjusted object, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to make the adjustment
ArithmeticException:
if numeric overflow occurs
atMonthback to summary
public MonthDay atMonth(Month month)

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.

Parameters
month:Month

the month-of-year to use, not null

Returns:MonthDay

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

atMonthback to summary
public MonthDay atMonth(int month)

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.

Parameters
month:int

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

Returns:MonthDay

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

Exceptions
DateTimeException:
if the month is invalid
atYearMonthback to summary
public LocalDate atYearMonth(YearMonth yearMonth)

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.

Parameters
yearMonth:YearMonth

the year-month to use, not null

Returns:LocalDate

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

compareToback to summary
public int compareTo(DayOfMonth other)

Implements java.lang.Comparable.compareTo.

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.

Parameters
other:DayOfMonth

the other day-of-month instance, not null

Returns:int

the comparator value, negative if less, positive if greater

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

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

Parameters
obj:Object

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

Returns:boolean

true if the day-of-month is the same

Annotations
@Override
fromback to summary
public static DayOfMonth from(TemporalAccessor temporal)

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

Parameters
temporal:TemporalAccessor

the temporal object to convert, not null

Returns:DayOfMonth

the day-of-month, not null

Exceptions
DateTimeException:
if unable to convert to a DayOfMonth
getback to summary
public int get(TemporalField field)

Overrides default java.time.temporal.TemporalAccessor.get.

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 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(TemporalAccessor) passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.

Parameters
field:TemporalField

the field to get, not null

Returns:int

the value for the field

Annotations
@Override
Exceptions
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
getLongback to summary
public long getLong(TemporalField field)

Implements java.time.temporal.TemporalAccessor.getLong.

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 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(TemporalAccessor) passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.

Parameters
field:TemporalField

the field to get, not null

Returns:long

the value for the field

Annotations
@Override
Exceptions
DateTimeException:
if a value for the field cannot be obtained
UnsupportedTemporalTypeException:
if the field is not supported
ArithmeticException:
if numeric overflow occurs
getValueback to summary
public int getValue()

Gets the day-of-month value.

Returns:int

the day-of-month, from 1 to 31

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

A hash code for this day-of-month.

Returns:int

a suitable hash code

Annotations
@Override
isSupportedback to summary
public boolean isSupported(TemporalField field)

Implements java.time.temporal.TemporalAccessor.isSupported.

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

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

  • 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(TemporalAccessor) passing this as the argument. Whether the field is supported is determined by the field.

Parameters
field:TemporalField

the field to check, null returns false

Returns:boolean

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

Annotations
@Override
isValidYearMonthback to summary
public boolean isValidYearMonth(YearMonth yearMonth)

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.

Parameters
yearMonth:YearMonth

the year month to validate, null returns false

Returns:boolean

true if the year and month are valid for this day

nowback to summary
public static DayOfMonth now()

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

This will query the 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.

Returns:DayOfMonth

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

nowback to summary
public static DayOfMonth now(ZoneId zone)

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

This will query the 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.

Parameters
zone:ZoneId

the zone ID to use, not null

Returns:DayOfMonth

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

nowback to summary
public static DayOfMonth now(Clock clock)

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

Parameters
clock:Clock

the clock to use, not null

Returns:DayOfMonth

the current day-of-month, not null

ofback to summary
public static DayOfMonth of(int dayOfMonth)

Obtains an instance of DayOfMonth.

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

Parameters
dayOfMonth:int

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

Returns:DayOfMonth

the day-of-month, not null

Exceptions
DateTimeException:
if the day-of-month is invalid
queryback to summary
public <R> R query(TemporalQuery<R> query)

Overrides default java.time.temporal.TemporalAccessor.query.

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(TemporalAccessor) method on the specified query passing this as the argument.

Parameters
<R>
the type of the result
query:TemporalQuery<R>

the query to invoke, not null

Returns:R

the query result, null may be returned (defined by the query)

Annotations
@SuppressWarnings:unchecked
@Override
Exceptions
DateTimeException:
if unable to query (defined by the query)
ArithmeticException:
if numeric overflow occurs (defined by the query)
rangeback to summary
public ValueRange range(TemporalField field)

Overrides default java.time.temporal.TemporalAccessor.range.

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 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(TemporalAccessor) passing this as the argument. Whether the range can be obtained is determined by the field.

Parameters
field:TemporalField

the field to query the range for, not null

Returns:ValueRange

the range of valid values for the field, not null

Annotations
@Override
Exceptions
DateTimeException:
if the range for the field cannot be obtained
UnsupportedTemporalTypeException:
if the field is not supported
readResolveback to summary
private Object readResolve()

Validates the input.

Returns:Object

the valid object, not null

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Outputs this day-of-month as a String.

Returns:String

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

Annotations
@Override