|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.time.calendar.MonthDay
public final class MonthDay
A month-day in the ISO-8601 calendar system, such as --12-03
.
MonthDay
is an immutable calendrical that represents the combination
of a year and month. Any field that can be derived from a month and day, such as
quarter-of-year, can be obtained.
This class does not store or represent a year, time or time-zone.
For example, the value "3rd December" can be stored in a MonthDay
.
Since a MonthDay
does not possess a year, the leap day of
29th of February is considered valid.
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 todays's rules for leap years are applied for all time.
For most applications written today, the ISO-8601 rules are entirely suitable.
Any application that uses historical dates should consider using HistoricDate
.
MonthDay is immutable and thread-safe.
Method Summary | ||
---|---|---|
LocalDate |
adjustDate(LocalDate date)
Adjusts a date to have the value of this month-day, returning a new date. |
|
LocalDate |
adjustDate(LocalDate date,
DateResolver resolver)
Adjusts a date to have the value of this month-day, using a resolver to handle the case when the day-of-month becomes invalid. |
|
LocalDate |
atYear(int year)
Returns a date formed from this month-day at the specified year. |
|
int |
compareTo(MonthDay other)
Compares this month-day to another month-day. |
|
boolean |
equals(java.lang.Object obj)
Checks if this month-day is equal to another month-day. |
|
static MonthDay |
from(Calendrical... calendricals)
Obtains an instance of MonthDay from a set of calendricals. |
|
|
get(CalendricalRule<T> ruleToDerive)
Gets the value of the specified calendrical rule. |
|
int |
getDayOfMonth()
Gets the day-of-month field. |
|
MonthOfYear |
getMonthOfYear()
Gets the month-of-year field, which is an enum MonthOfYear . |
|
int |
hashCode()
A hash code for this month-day. |
|
boolean |
isAfter(MonthDay other)
Is this month-day after the specified month-day. |
|
boolean |
isBefore(MonthDay other)
Is this month-day before the specified month-day. |
|
boolean |
isValidYear(int year)
Checks if the year is valid for this month-day. |
|
boolean |
matchesCalendrical(Calendrical calendrical)
Checks if the month-day extracted from the calendrical matches this. |
|
static MonthDay |
now()
Obtains the current month-day from the system clock in the default time-zone. |
|
static MonthDay |
now(Clock clock)
Obtains the current month-day from the specified clock. |
|
static MonthDay |
of(int monthOfYear,
int dayOfMonth)
Obtains an instance of MonthDay . |
|
static MonthDay |
of(MonthOfYear monthOfYear,
int dayOfMonth)
Obtains an instance of MonthDay . |
|
static MonthDay |
parse(java.lang.CharSequence text)
Obtains an instance of MonthDay from a text string such as --12-03 . |
|
static MonthDay |
parse(java.lang.CharSequence text,
DateTimeFormatter formatter)
Obtains an instance of MonthDay from a text string using a specific formatter. |
|
MonthDay |
rollDayOfMonth(int days)
Rolls the day-of-month, adding the specified number of days to a copy of this MonthDay . |
|
MonthDay |
rollMonthOfYear(int months)
Rolls the month-of-year, adding the specified number of months to a copy of this MonthDay . |
|
static CalendricalRule<MonthDay> |
rule()
Gets the rule for MonthDay . |
|
DateTimeFields |
toFields()
Converts this month-day to an equivalent fields object. |
|
java.lang.String |
toString()
Outputs this month-day as a String , such as --12-03 . |
|
java.lang.String |
toString(DateTimeFormatter formatter)
Outputs this month-day as a String using the formatter. |
|
MonthDay |
with(MonthOfYear monthOfYear)
Returns a copy of this MonthDay with the month-of-year altered. |
|
MonthDay |
withDayOfMonth(int dayOfMonth)
Returns a copy of this MonthDay with the day-of-month altered. |
|
MonthDay |
withMonthOfYear(int monthOfYear)
Returns a copy of this MonthDay with the month-of-year altered. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public static CalendricalRule<MonthDay> rule()
MonthDay
.
public static MonthDay now()
This will query the system clock
in the default
time-zone to obtain the current month-day.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
public static MonthDay now(Clock clock)
This will query the specified clock to obtain the current month-day.
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 null
public static MonthDay of(MonthOfYear monthOfYear, int dayOfMonth)
MonthDay
.
The day-of-month must be valid for the month within a leap year. Hence, for February, day 29 is valid.
For example, passing in April and day 31 will throw an exception, as there can never be a 31st April in any year. Alternately, passing in 29th February is valid, as that month-day can be valid.
monthOfYear
- the month-of-year to represent, not nulldayOfMonth
- the day-of-month to represent, from 1 to 31
IllegalCalendarFieldValueException
- if the value of any field is out of range
InvalidCalendarFieldException
- if the day-of-month is invalid for the monthpublic static MonthDay of(int monthOfYear, int dayOfMonth)
MonthDay
.
The day-of-month must be valid for the month within a leap year. Hence, for month 2 (February), day 29 is valid.
For example, passing in month 4 (April) and day 31 will throw an exception, as there can never be a 31st April in any year. Alternately, passing in 29th February is valid, as that month-day can be valid.
monthOfYear
- the month-of-year to represent, from 1 (January) to 12 (December)dayOfMonth
- the day-of-month to represent, from 1 to 31
IllegalCalendarFieldValueException
- if the value of any field is out of range
InvalidCalendarFieldException
- if the day-of-month is invalid for the monthpublic static MonthDay from(Calendrical... calendricals)
MonthDay
from a set of calendricals.
A calendrical represents some form of date and time information. This method combines the input calendricals into a month-day.
calendricals
- the calendricals to create a month-day from, no nulls, not null
CalendricalException
- if unable to merge to a month-daypublic static MonthDay parse(java.lang.CharSequence text)
MonthDay
from a text string such as --12-03
.
The string must represent a valid month-day.
The format is --MM-dd
.
text
- the text to parse such as "--12-03", not null
CalendricalParseException
- if the text cannot be parsedpublic static MonthDay parse(java.lang.CharSequence text, DateTimeFormatter formatter)
MonthDay
from a text string using a specific formatter.
The text is parsed using the formatter, returning a month-day.
text
- the text to parse, not nullformatter
- the formatter to use, not null
java.lang.UnsupportedOperationException
- if the formatter cannot parse
CalendricalParseException
- if the text cannot be parsedpublic <T> T get(CalendricalRule<T> ruleToDerive)
This method queries the value of the specified calendrical rule.
If the value cannot be returned for the rule from this month-day then
null
will be returned.
get
in interface Calendrical
ruleToDerive
- the rule to derive, not null
public MonthOfYear getMonthOfYear()
MonthOfYear
.
This method returns the enum MonthOfYear
for the month.
This avoids confusion as to what int
values mean.
If you need access to the primitive int
value then the enum
provides the int value
.
Additional information can be obtained from the MonthOfYear
.
This includes month lengths, textual names and access to the quarter-of-year
and month-of-quarter values.
public int getDayOfMonth()
This method returns the primitive int
value for the day-of-month.
public MonthDay with(MonthOfYear monthOfYear)
MonthDay
with the month-of-year altered.
If the day-of-month is invalid for the specified month, the day will be adjusted to the last valid day-of-month.
This instance is immutable and unaffected by this method call.
monthOfYear
- the month-of-year to set in the returned month-day, not null
MonthDay
based on this month-day with the requested month, not nullpublic MonthDay withMonthOfYear(int monthOfYear)
MonthDay
with the month-of-year altered.
If the day-of-month is invalid for the specified month, the day will be adjusted to the last valid day-of-month.
This instance is immutable and unaffected by this method call.
monthOfYear
- the month-of-year to set in the returned month-day, from 1 (January) to 12 (December)
MonthDay
based on this month-day with the requested month, not null
IllegalCalendarFieldValueException
- if the month-of-year value is invalidpublic MonthDay withDayOfMonth(int dayOfMonth)
MonthDay
with the day-of-month altered.
If the day-of-month is invalid for the current month, an exception will be thrown.
This instance is immutable and unaffected by this method call.
dayOfMonth
- the day-of-month to set in the return month-day, from 1 to 31
MonthDay
based on this month-day with the requested day, not null
IllegalCalendarFieldValueException
- if the day-of-month value is invalid
InvalidCalendarFieldException
- if the day-of-month is invalid for the monthpublic MonthDay rollMonthOfYear(int months)
MonthDay
.
This method will add the specified number of months to the month-day, rolling from December back to January if necessary.
If the day-of-month is invalid for the specified month in the result, the day will be adjusted to the last valid day-of-month.
This instance is immutable and unaffected by this method call.
months
- the months to roll by, positive or negative
MonthDay
based on this month-day with the month rolled, not nullpublic MonthDay rollDayOfMonth(int days)
MonthDay
.
This method will add the specified number of days to the month-day, rolling from last day-of-month to the first if necessary.
This instance is immutable and unaffected by this method call.
days
- the days to roll by, positive or negative
MonthDay
based on this month-day with the day rolled, not nullpublic boolean matchesCalendrical(Calendrical calendrical)
This method implements the CalendricalMatcher
interface.
It is intended that applications use LocalDate.matches(javax.time.calendar.CalendricalMatcher)
rather than this method.
matchesCalendrical
in interface CalendricalMatcher
calendrical
- the calendrical to match, not null
public LocalDate adjustDate(LocalDate date)
This method implements the DateAdjuster
interface.
It is intended that, instead of calling this method directly, it is used from
an instance of LocalDate
:
date = date.with(monthDay);
This implementation handles the case where this represents February 29 and the year is not a leap year by throwing an exception.
This instance is immutable and unaffected by this method call.
adjustDate
in interface DateAdjuster
date
- the date to be adjusted, not null
InvalidCalendarFieldException
- if the day-of-month is invalid for the yearpublic LocalDate adjustDate(LocalDate date, DateResolver resolver)
This instance is immutable and unaffected by this method call.
date
- the date to be adjusted, not nullresolver
- the date resolver to use if the day-of-month is invalid, not null
InvalidCalendarFieldException
- if the day-of-month is invalid for the yearpublic boolean isValidYear(int year)
This method checks whether this month and day and the input year form a valid date.
year
- the year to validate, an out of range value returns false
Year.isValidMonthDay(MonthDay)
public LocalDate atYear(int year)
This method merges this
and the specified year to form an
instance of LocalDate
.
LocalDate date = monthDay.atYear(year);
This instance is immutable and unaffected by this method call.
year
- the year to use, from MIN_YEAR to MAX_YEAR
Year.atMonthDay(MonthDay)
public DateTimeFields toFields()
The fields will contain ISODateTimeRule.MONTH_OF_YEAR
and
ISODateTimeRule.DAY_OF_MONTH
.
public int compareTo(MonthDay other)
compareTo
in interface java.lang.Comparable<MonthDay>
other
- the other month-day to compare to, not null
java.lang.NullPointerException
- if other
is nullpublic boolean isAfter(MonthDay other)
other
- the other month-day to compare to, not null
java.lang.NullPointerException
- if other
is nullpublic boolean isBefore(MonthDay other)
other
- the other month-day to compare to, not null
java.lang.NullPointerException
- if other
is nullpublic boolean equals(java.lang.Object obj)
The comparison is based on the time-line position of the month-day within a year.
equals
in class java.lang.Object
obj
- the object to check, null returns false
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
String
, such as --12-03
.
The output will be in the format --MM-dd
:
toString
in class java.lang.Object
public java.lang.String toString(DateTimeFormatter formatter)
String
using the formatter.
formatter
- the formatter to use, not null
java.lang.UnsupportedOperationException
- if the formatter cannot print
CalendricalException
- if an error occurs during printing
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |