java.io.Serializable
, java.lang.Comparable<DayOfMonth>
, java.time.temporal.TemporalAccessor
, java.time.temporal.TemporalAdjuster
public final class DayOfMonth extends java.lang.Object implements java.time.temporal.TemporalAccessor, java.time.temporal.TemporalAdjuster, java.lang.Comparable<DayOfMonth>, java.io.Serializable
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.
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 ==.
Modifier and Type | Method | Description |
---|---|---|
java.time.temporal.Temporal |
adjustInto(java.time.temporal.Temporal temporal) |
Adjusts the specified temporal object to have this day-of-month.
|
java.time.MonthDay |
atMonth(int month) |
Combines this day-of-month with a month to create a
MonthDay . |
java.time.MonthDay |
atMonth(java.time.Month month) |
Combines this day-of-month with a month to create a
MonthDay . |
java.time.LocalDate |
atYearMonth(java.time.YearMonth yearMonth) |
Combines this day-of-month with a year-month to create a
LocalDate . |
int |
compareTo(DayOfMonth other) |
Compares this day-of-month to another.
|
boolean |
equals(java.lang.Object obj) |
Checks if this day-of-month is equal to another day-of-month.
|
static DayOfMonth |
from(java.time.temporal.TemporalAccessor temporal) |
Obtains an instance of
DayOfMonth from a date-time object. |
int |
get(java.time.temporal.TemporalField field) |
Gets the value of the specified field from this day-of-month as an
int . |
long |
getLong(java.time.temporal.TemporalField field) |
Gets the value of the specified field from this day-of-month as a
long . |
int |
getValue() |
Gets the day-of-month value.
|
int |
hashCode() |
A hash code for this day-of-month.
|
boolean |
isSupported(java.time.temporal.TemporalField field) |
Checks if the specified field is supported.
|
boolean |
isValidYearMonth(java.time.YearMonth yearMonth) |
Checks if the year-month is valid for this year.
|
static DayOfMonth |
now() |
Obtains the current day-of-month from the system clock in the default time-zone.
|
static DayOfMonth |
now(java.time.Clock clock) |
Obtains the current day-of-month from the specified clock.
|
static DayOfMonth |
now(java.time.ZoneId zone) |
Obtains the current day-of-month from the system clock in the specified time-zone.
|
static DayOfMonth |
of(int dayOfMonth) |
Obtains an instance of
DayOfMonth . |
<R> R |
query(java.time.temporal.TemporalQuery<R> query) |
Queries this day-of-month using the specified query.
|
java.time.temporal.ValueRange |
range(java.time.temporal.TemporalField field) |
Gets the range of valid values for the specified field.
|
java.lang.String |
toString() |
Outputs this day-of-month as a
String . |
public static DayOfMonth now()
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.
public static DayOfMonth now(java.time.ZoneId 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.
zone
- the zone ID to use, not nullpublic static DayOfMonth now(java.time.Clock 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
.
clock
- the clock to use, not nullpublic static DayOfMonth of(int dayOfMonth)
DayOfMonth
.
A day-of-month object represents one of the 31 days of the month, from 1 to 31.
dayOfMonth
- the day-of-month to represent, from 1 to 31java.time.DateTimeException
- if the day-of-month is invalidpublic static DayOfMonth from(java.time.temporal.TemporalAccessor temporal)
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
.
temporal
- the temporal object to convert, not nulljava.time.DateTimeException
- if unable to convert to a DayOfMonth
public int getValue()
public boolean isSupported(java.time.temporal.TemporalField field)
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
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.
isSupported
in interface java.time.temporal.TemporalAccessor
field
- the field to check, null returns falsepublic java.time.temporal.ValueRange range(java.time.temporal.TemporalField 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.
range
in interface java.time.temporal.TemporalAccessor
field
- the field to query the range for, not nulljava.time.DateTimeException
- if the range for the field cannot be obtainedjava.time.temporal.UnsupportedTemporalTypeException
- if the field is not supportedpublic int get(java.time.temporal.TemporalField field)
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.
get
in interface java.time.temporal.TemporalAccessor
field
- the field to get, not nulljava.time.DateTimeException
- if a value for the field cannot be obtained or
the value is outside the range of valid values for the fieldjava.time.temporal.UnsupportedTemporalTypeException
- if the field is not supported or
the range of values exceeds an int
java.lang.ArithmeticException
- if numeric overflow occurspublic long getLong(java.time.temporal.TemporalField field)
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.
getLong
in interface java.time.temporal.TemporalAccessor
field
- the field to get, not nulljava.time.DateTimeException
- if a value for the field cannot be obtainedjava.time.temporal.UnsupportedTemporalTypeException
- if the field is not supportedjava.lang.ArithmeticException
- if numeric overflow occurspublic boolean isValidYearMonth(java.time.YearMonth yearMonth)
This method checks whether this day and the input year and month form a valid date.
yearMonth
- the year month to validate, null returns falsepublic <R> R query(java.time.temporal.TemporalQuery<R> 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.
query
in interface java.time.temporal.TemporalAccessor
R
- the type of the resultquery
- the query to invoke, not nulljava.time.DateTimeException
- if unable to query (defined by the query)java.lang.ArithmeticException
- if numeric overflow occurs (defined by the query)public java.time.temporal.Temporal adjustInto(java.time.temporal.Temporal temporal)
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.
adjustInto
in interface java.time.temporal.TemporalAdjuster
temporal
- the target object to be adjusted, not nulljava.time.DateTimeException
- if unable to make the adjustmentjava.lang.ArithmeticException
- if numeric overflow occurspublic java.time.MonthDay atMonth(java.time.Month month)
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.
month
- the month-of-year to use, not nullpublic java.time.MonthDay atMonth(int month)
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.
month
- the month-of-year to use, from 1 (January) to 12 (December)java.time.DateTimeException
- if the month is invalidpublic java.time.LocalDate atYearMonth(java.time.YearMonth yearMonth)
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.
yearMonth
- the year-month to use, not nullpublic int compareTo(DayOfMonth other)
The comparison is based on the value of the day.
It is "consistent with equals", as defined by Comparable
.
compareTo
in interface java.lang.Comparable<DayOfMonth>
other
- the other day-of-month instance, not nullpublic boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
obj
- the other day-of-month instance, null returns falsepublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
String
.toString
in class java.lang.Object
Copyright © 2010–2018 ThreeTen.org. All rights reserved.