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.
This is a value-based
class; programmers should treat instances that are
equal as interchangeable and should not
use instances for synchronization, or unpredictable behavior may
occur. For example, in a future release, synchronization may fail.
The equals
method should be used for comparisons.
Implementation Specification
This class is immutable and thread-safe.
Modifier and Type | Field and Description |
---|---|
private final int | month
The month-of-year, not null. |
private static final DateTimeFormatter | PARSER
Parser. |
private static final long | serialVersionUID
Serialization version. |
private final int | year
The year. |
Access | Constructor and Description |
---|---|
private | YearMonth(int
the year to represent, validated from MIN_YEAR to MAX_YEAR year, int the month-of-year to represent, validated from 1 (January) to 12 (December) month)Constructor. |
Modifier and Type | Method and Description |
---|---|
public Temporal | Returns: the adjusted object, not nullthe target object to be adjusted, not null temporal)Implements java. Adjusts the specified temporal object to have this year-month. |
public LocalDate | Returns: the date formed from this year-month and the specified day, not nullthe day-of-month to use, from 1 to 31 dayOfMonth)Combines this year-month with a day-of-month to create a |
public LocalDate | Returns: the last valid date of this year-month, not nullReturns a |
public int | Returns: the comparator value, that is less than zero if this is beforeother ,
zero if they are equal, greater than zero if this is after other the other year-month to compare to, not null other)Implements java. Compares this year-month to another year-month. |
public boolean | Returns: true if this is equal to the other year-monththe object to check, null returns false obj)Overrides java. Checks if this year-month is equal to another year-month. |
public String | Returns: the formatted year-month string, not nullthe formatter to use, not null formatter)Formats this year-month using the specified formatter. |
public static YearMonth | Returns: the year-month, not nullthe temporal object to convert, not null temporal)Obtains an instance of |
public int | Returns: the value for the fieldthe field to get, not null field)Overrides default java. Gets the value of the specified field from this year-month as an |
public long | Returns: the value for the fieldthe field to get, not null field)Implements java. Gets the value of the specified field from this year-month as a |
public Month | |
public int | |
private long | |
public int | |
public int | Returns: a suitable hash codeOverrides java. A hash code for this year-month. |
public boolean | |
public boolean | |
public boolean | Returns: true if the year is leap, false otherwiseChecks if the year is a leap year, according to the ISO proleptic calendar system rules. |
public boolean | Returns: true if the field is supported on this year-month, false if notthe field to check, null returns false field)Implements java. Checks if the specified field is supported. |
public boolean | Returns: true if the unit can be added/subtracted, false if notthe unit to check, null returns false unit)Implements java. Checks if the specified unit is supported. |
public boolean | Returns: true if the day is valid for this year-monththe day-of-month to validate, from 1 to 31, invalid value returns false dayOfMonth)Checks if the day-of-month is valid for this year-month. |
public int | Returns: the length of the month in days, from 28 to 31Returns the length of the month, taking account of the year. |
public int | |
public YearMonth | Returns: aYearMonth based on this year-month with the subtraction made, not nullthe amount to subtract, not null amountToSubtract)Overrides default java. Returns a copy of this year-month with the specified amount subtracted. |
public YearMonth | Returns: aYearMonth based on this year-month with the specified amount subtracted, not nullthe amount of the unit to subtract from the result, may be negative amountToSubtract, TemporalUnit the unit of the amount to subtract, not null unit)Overrides default java. Returns a copy of this year-month with the specified amount subtracted. |
public YearMonth | Returns: aYearMonth based on this year-month with the months subtracted, not nullthe months to subtract, may be negative monthsToSubtract)Returns a copy of this |
public YearMonth | Returns: aYearMonth based on this year-month with the years subtracted, not nullthe years to subtract, may be negative yearsToSubtract)Returns a copy of this |
public static YearMonth | Returns: the current year-month using the system clock and default time-zone, not nullObtains the current year-month from the system clock in the default time-zone. |
public static YearMonth | |
public static YearMonth | |
public static YearMonth | |
public static YearMonth | Returns: the year-month, not nullthe year to represent, from MIN_YEAR to MAX_YEAR year, int the month-of-year to represent, from 1 (January) to 12 (December) month)Obtains an instance of |
public static YearMonth | Returns: the parsed year-month, not nullthe text to parse such as "2007-12", not null text)Obtains an instance of |
public static YearMonth | Returns: the parsed year-month, not nullthe text to parse, not null text, DateTimeFormatter the formatter to use, not null formatter)Obtains an instance of |
public YearMonth | Returns: aYearMonth based on this year-month with the addition made, not nullthe amount to add, not null amountToAdd)Overrides default java. Returns a copy of this year-month with the specified amount added. |
public YearMonth | Returns: aYearMonth based on this year-month with the specified amount added, not nullthe amount of the unit to add to the result, may be negative amountToAdd, TemporalUnit the unit of the amount to add, not null unit)Implements java. Returns a copy of this year-month with the specified amount added. |
public YearMonth | Returns: aYearMonth based on this year-month with the months added, not nullthe months to add, may be negative monthsToAdd)Returns a copy of this |
public YearMonth | Returns: aYearMonth based on this year-month with the years added, not nullthe years to add, may be negative yearsToAdd)Returns a copy of this |
public < the type of the result R> R | Returns: the query result, null may be returned (defined by the query)the query to invoke, not null query)Overrides default java. Queries this year-month using the specified query. |
public ValueRange | Returns: the range of valid values for the field, not nullthe field to query the range for, not null field)Overrides default java. Gets the range of valid values for the specified field. |
pack-priv static YearMonth | |
private void | |
public String | Returns: a string representation of this year-month, not nullOverrides java. Outputs this year-month as a |
public long | Returns: the amount of time between this year-month and the end year-monththe end date, exclusive, which is converted to a endExclusive, TemporalUnit YearMonth , not nullthe unit to measure the amount in, not null unit)Implements java. Calculates the amount of time until another year-month in terms of the specified unit. |
private YearMonth | Returns: the year-month, not nullthe year to represent, validated from MIN_YEAR to MAX_YEAR newYear, int the month-of-year to represent, validated not null newMonth)Returns a copy of this year-month with the new year and month, checking to see if a new object is in fact required. |
public YearMonth | Returns: aYearMonth based on this with the adjustment made, not nullthe adjuster to use, not null adjuster)Overrides default java. Returns an adjusted copy of this year-month. |
public YearMonth | Returns: aYearMonth based on this with the specified field set, not nullthe field to set in the result, not null field, long the new value of the field in the result newValue)Implements java. Returns a copy of this year-month with the specified field set to a new value. |
public YearMonth | Returns: aYearMonth based on this year-month with the requested month, not nullthe month-of-year to set in the returned year-month, from 1 (January) to 12 (December) month)Returns a copy of this |
public YearMonth | Returns: aYearMonth based on this year-month with the requested year, not nullthe year to set in the returned year-month, from MIN_YEAR to MAX_YEAR year)Returns a copy of this |
pack-priv void | |
private Object | Returns: the instance ofSer , not nullWrites the object using a dedicated serialized form. |
month | back to summary |
---|---|
private final int month The month-of-year, not null. |
PARSER | back to summary |
---|---|
private static final DateTimeFormatter PARSER Parser. |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID Serialization version. |
year | back to summary |
---|---|
private final int year The year. |
YearMonth | back to summary |
---|---|
private YearMonth(int year, int month) Constructor.
|
adjustInto | back to summary |
---|---|
public Temporal adjustInto(Temporal temporal) Implements java. 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
In most cases, it is clearer to reverse the calling pattern by using
// 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.
|
atDay | back to summary |
---|---|
public LocalDate atDay(int dayOfMonth) Combines this year-month with a day-of-month to create a
This returns a 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);
|
atEndOfMonth | back to summary |
---|---|
public LocalDate atEndOfMonth() Returns a
This returns a This method can be used as part of a chain to produce a date: LocalDate date = year.atMonth(month).atEndOfMonth();
|
compareTo | back to summary |
---|---|
public int compareTo(YearMonth other) Implements java. 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 |
equals | back to summary |
---|---|
public boolean equals(Object obj) Overrides java. Checks if this year-month is equal to another year-month. The comparison is based on the time-line position of the year-months. |
format | back to summary |
---|---|
public String format(DateTimeFormatter formatter) Formats this year-month using the specified formatter. This year-month will be passed to the formatter to produce a string.
|
from | back to summary |
---|---|
public static YearMonth from(TemporalAccessor temporal) Obtains an instance of
This obtains a year-month based on the specified temporal.
A
The conversion extracts the
This method matches the signature of the functional interface
|
get | back to summary |
---|---|
public int get(TemporalField field) Overrides default java. Gets the value of the specified field from this year-month as an This queries this year-month for the value of 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
If the field is not a
|
getLong | back to summary |
---|---|
public long getLong(TemporalField field) Implements java. Gets the value of the specified field from this year-month as a This queries this year-month for the value of 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
If the field is not a
|
getMonth | back to summary |
---|---|
public Month getMonth() Gets the month-of-year field using the
This method returns the enum
|
getMonthValue | back to summary |
---|---|
public int getMonthValue() Gets the month-of-year field from 1 to 12.
This method returns the month as an
|
getProlepticMonth | back to summary |
---|---|
private long getProlepticMonth() |
getYear | back to summary |
---|---|
public int getYear() Gets the year field.
This method returns the primitive
The year returned by this method is proleptic as per
|
hashCode | back to summary |
---|---|
public int hashCode() Overrides java. A hash code for this year-month.
|
isAfter | back to summary |
---|---|
public boolean isAfter(YearMonth other) Checks if this year-month is after the specified year-month.
|
isBefore | back to summary |
---|---|
public boolean isBefore(YearMonth other) Checks if this year-month is before the specified year-month.
|
isLeapYear | back to summary |
---|---|
public boolean isLeapYear() 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.
|
isSupported | back to summary |
---|---|
public boolean isSupported(TemporalField field) Implements java. Checks if the specified field is supported.
This checks if this year-month can be queried for the specified field.
If false, then calling the
If the field is a
ChronoField instances will return false.
If the field is not a
|
isSupported | back to summary |
---|---|
public boolean isSupported(TemporalUnit unit) Implements java. Checks if the specified unit is supported.
This checks if the specified unit can be added to, or subtracted from, this year-month.
If false, then calling the
If the unit is a
ChronoUnit instances will return false.
If the unit is not a
|
isValidDay | back to summary |
---|---|
public boolean isValidDay(int dayOfMonth) 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.
|
lengthOfMonth | back to summary |
---|---|
public int lengthOfMonth() 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.
|
lengthOfYear | back to summary |
---|---|
public int lengthOfYear() Returns the length of the year. This returns the length of the year in days, either 365 or 366.
|
minus | back to summary |
---|---|
public YearMonth minus(TemporalAmount amountToSubtract) Overrides default java. Returns a copy of this year-month with the specified amount subtracted.
This returns a
The calculation is delegated to the amount object by calling
This instance is immutable and unaffected by this method call.
|
minus | back to summary |
---|---|
public YearMonth minus(long amountToSubtract, TemporalUnit unit) Overrides default java. Returns a copy of this year-month with the specified amount subtracted.
This returns a
This method is equivalent to This instance is immutable and unaffected by this method call.
|
minusMonths | back to summary |
---|---|
public YearMonth minusMonths(long monthsToSubtract) Returns a copy of this This instance is immutable and unaffected by this method call.
|
minusYears | back to summary |
---|---|
public YearMonth minusYears(long yearsToSubtract) Returns a copy of this This instance is immutable and unaffected by this method call.
|
now | back to summary |
---|---|
public static YearMonth now() Obtains the current year-month from the system clock in the default time-zone.
This will query the Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
|
now | back to summary |
---|---|
public static YearMonth now(ZoneId zone) Obtains the current year-month from the system clock in the specified time-zone.
This will query the Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded. |
now | back to summary |
---|---|
public static YearMonth now(Clock clock) 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 |
of | back to summary |
---|---|
public static YearMonth of(int year, Month month) Obtains an instance of
|
of | back to summary |
---|---|
public static YearMonth of(int year, int month) Obtains an instance of
|
parse | back to summary |
---|---|
public static YearMonth parse(CharSequence text) Obtains an instance of
The string must represent a valid year-month.
The format must be
|
parse | back to summary |
---|---|
public static YearMonth parse(CharSequence text, DateTimeFormatter formatter) Obtains an instance of The text is parsed using the formatter, returning a year-month.
|
plus | back to summary |
---|---|
public YearMonth plus(TemporalAmount amountToAdd) Overrides default java. Returns a copy of this year-month with the specified amount added.
This returns a
The calculation is delegated to the amount object by calling
This instance is immutable and unaffected by this method call.
|
plus | back to summary |
---|---|
public YearMonth plus(long amountToAdd, TemporalUnit unit) Implements java. Returns a copy of this year-month with the specified amount added.
This returns a
If the field is a
All other
If the field is not a This instance is immutable and unaffected by this method call.
|
plusMonths | back to summary |
---|---|
public YearMonth plusMonths(long monthsToAdd) Returns a copy of this This instance is immutable and unaffected by this method call.
|
plusYears | back to summary |
---|---|
public YearMonth plusYears(long yearsToAdd) Returns a copy of this This instance is immutable and unaffected by this method call.
|
query | back to summary |
---|---|
public <R> R query(TemporalQuery<R> query) Overrides default java. Queries this year-month using the specified query.
This queries this year-month using the specified query strategy object.
The
The result of this method is obtained by invoking the
|
range | back to summary |
---|---|
public ValueRange range(TemporalField field) Overrides default java. 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
If the field is not a
|
readExternal | back to summary |
---|---|
pack-priv static YearMonth readExternal(DataInput in) throws IOException |
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws InvalidObjectException Defend against malicious streams.
|
toString | back to summary |
---|---|
public String toString() Overrides java. Outputs this year-month as a
The output will be in the format |
until | back to summary |
---|---|
public long until(Temporal endExclusive, TemporalUnit unit) Implements java. Calculates the amount of time until another year-month in terms of the specified unit.
This calculates the amount of time between two The calculation returns a whole number, representing the number of complete units between the two year-months. For example, the amount in decades between 2012-06 and 2032-05 will only be one decade as it is one month short of two decades.
There are two equivalent ways of using this method.
The first is to invoke this method.
The second is to use // these two lines are equivalent amount = start.until(end, MONTHS); amount = MONTHS.between(start, end);The choice should be made based on which makes the code more readable.
The calculation is implemented in this method for
If the unit is not a This instance is immutable and unaffected by this method call.
|
with | back to summary |
---|---|
private YearMonth with(int newYear, int newMonth) Returns a copy of this year-month with the new year and month, checking to see if a new object is in fact required.
|
with | back to summary |
---|---|
public YearMonth with(TemporalAdjuster adjuster) Overrides default java. Returns an adjusted copy of this year-month.
This returns a A simple adjuster might simply set the one of the fields, such as the year field. A more complex adjuster might set the year-month to the next month that Halley's comet will pass the Earth.
The result of this method is obtained by invoking the
This instance is immutable and unaffected by this method call.
|
with | back to summary |
---|---|
public YearMonth with(TemporalField field, long newValue) Implements java. Returns a copy of this year-month with the specified field set to a new value.
This returns a
If the field is a
In all cases, if the new value is outside the valid range of values for the field
then a
All other
If the field is not a This instance is immutable and unaffected by this method call.
|
withMonth | back to summary |
---|---|
public YearMonth withMonth(int month) Returns a copy of this This instance is immutable and unaffected by this method call.
|
withYear | back to summary |
---|---|
public YearMonth withYear(int year) Returns a copy of this This instance is immutable and unaffected by this method call.
|
writeExternal | back to summary |
---|---|
pack-priv void writeExternal(DataOutput out) throws IOException |
writeReplace | back to summary |
---|---|
private Object writeReplace() Writes the object using a dedicated serialized form. |