2007
.
Year
is an immutable date-time object that represents a year.
Any field that can be derived from a year can be obtained.
Note that years in the ISO chronology only align with years in the Gregorian-Julian system for modern years. Parts of Russia did not switch to the modern Gregorian/ISO rules until 1920. As such, historical years must be treated with caution.
This class does not store or represent a month, day, time or time-zone.
For example, the value "2007" can be stored in a Year
.
Years represented by this class follow the ISO-8601 standard and use the proleptic numbering system. Year 1 is preceded by year 0, then by year -1.
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 |
---|---|
public static final int | MAX_VALUE
The maximum supported year, '+999,999,999'. |
public static final int | MIN_VALUE
The minimum supported year, '-999,999,999'. |
private static final DateTimeFormatter | PARSER
Parser. |
private static final long | serialVersionUID
Serialization version. |
private final int | year
The year being represented. |
Access | Constructor and Description |
---|---|
private |
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. |
public LocalDate | Returns: the local date formed from this year and the specified date of year, not nullthe day-of-year to use, from 1 to 365-366 dayOfYear)Combines this year with a day-of-year to create a |
public YearMonth | |
public YearMonth | Returns: the year-month formed from this year and the specified month, not nullthe month-of-year to use, from 1 (January) to 12 (December) month)Combines this year with a month to create a |
public LocalDate | Returns: the local date formed from this year and the specified month-day, not nullthe month-day to use, not null monthDay)Combines this year with a month-day to create a |
public int | Returns: the comparator value, that is less than zero if this is beforeother ,
zero if they are equal, or greater than zero if this is after other the other year to compare to, not null other)Implements java. Compares this year to another year. |
public boolean | Returns: true if this is equal to the other yearthe object to check, null returns false obj)Overrides java. Checks if this year is equal to another year. |
public String | Returns: the formatted year string, not nullthe formatter to use, not null formatter)Formats this year using the specified formatter. |
public static Year | Returns: the year, 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 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 as a |
public int | |
public int | Returns: a suitable hash codeOverrides java. A hash code for this year. |
public boolean | |
public boolean | |
public static boolean | Returns: true if the year is leap, false otherwisethe year to check year)Checks if the year is a leap year, according to the ISO proleptic calendar system rules. |
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, 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 month and day are valid for this yearthe month-day to validate, null returns false monthDay)Checks if the month-day is valid for this year. |
public int | |
public Year | Returns: aYear based on this year with the subtraction made, not nullthe amount to subtract, not null amountToSubtract)Overrides default java. Returns a copy of this year with the specified amount subtracted. |
public Year | Returns: aYear based on this year 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 with the specified amount subtracted. |
public Year | Returns: aYear based on this year with the year subtracted, not nullthe years to subtract, may be negative yearsToSubtract)Returns a copy of this |
public static Year | Returns: the current year using the system clock and default time-zone, not nullObtains the current year from the system clock in the default time-zone. |
public static Year | |
public static Year | |
public static Year | Returns: the year, not nullthe ISO proleptic year to represent, from isoYear)MIN_VALUE to MAX_VALUE Obtains an instance of |
public static Year | Returns: the parsed year, not nullthe text to parse such as "2007", not null text)Obtains an instance of |
public static Year | Returns: the parsed year, not nullthe text to parse, not null text, DateTimeFormatter the formatter to use, not null formatter)Obtains an instance of |
public Year | Returns: aYear based on this year with the addition made, not nullthe amount to add, not null amountToAdd)Overrides default java. Returns a copy of this year with the specified amount added. |
public Year | Returns: aYear based on this year 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 with the specified amount added. |
public Year | Returns: aYear based on this year 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 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 Year | |
private void | |
public String | Returns: a string representation of this year, not nullOverrides java. Outputs this year as a |
public long | Returns: the amount of time between this year and the end yearthe end date, exclusive, which is converted to a endExclusive, TemporalUnit Year , not nullthe unit to measure the amount in, not null unit)Implements java. Calculates the amount of time until another year in terms of the specified unit. |
public Year | Returns: aYear 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. |
public Year | Returns: aYear 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 with the specified field set to a new value. |
pack-priv void | |
private Object | Returns: the instance ofSer , not nullWrites the object using a dedicated serialized form. |
MAX_VALUE | back to summary |
---|---|
public static final int MAX_VALUE The maximum supported year, '+999,999,999'. |
MIN_VALUE | back to summary |
---|---|
public static final int MIN_VALUE The minimum supported year, '-999,999,999'. |
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 being represented. |
Year | back to summary |
---|---|
private Year(int year) Constructor.
|
adjustInto | back to summary |
---|---|
public Temporal adjustInto(Temporal temporal) Implements java. Adjusts the specified temporal object to have this year. This returns a temporal object of the same observable type as the input with the year 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 = thisYear.adjustInto(temporal); temporal = temporal.with(thisYear); This instance is immutable and unaffected by this method call.
|
atDay | back to summary |
---|---|
public LocalDate atDay(int dayOfYear) Combines this year with a day-of-year to create a
This returns a The day-of-year value 366 is only valid in a leap year.
|
atMonth | back to summary |
---|---|
public YearMonth atMonth(Month month) Combines this year with a month to create a
This returns a This method can be used as part of a chain to produce a date: LocalDate date = year.atMonth(month).atDay(day); |
atMonth | back to summary |
---|---|
public YearMonth atMonth(int month) Combines this year with a month to create a
This returns a This method can be used as part of a chain to produce a date: LocalDate date = year.atMonth(month).atDay(day);
|
atMonthDay | back to summary |
---|---|
public LocalDate atMonthDay(MonthDay monthDay) Combines this year with a month-day to create a
This returns a A month-day of February 29th will be adjusted to February 28th in the resulting date if the year is not a leap year. |
compareTo | back to summary |
---|---|
public int compareTo(Year other) Implements java. Compares this year to another year.
The comparison is based on the value of the year.
It is "consistent with equals", as defined by |
equals | back to summary |
---|---|
public boolean equals(Object obj) Overrides java. Checks if this year is equal to another year. The comparison is based on the time-line position of the years. |
format | back to summary |
---|---|
public String format(DateTimeFormatter formatter) Formats this year using the specified formatter. This year will be passed to the formatter to produce a string.
|
from | back to summary |
---|---|
public static Year from(TemporalAccessor temporal) Obtains an instance of
This obtains a year 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 as an This queries this year 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 as a This queries this year 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
|
getValue | back to summary |
---|---|
public int getValue() Gets the year value.
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.
|
isAfter | back to summary |
---|---|
public boolean isAfter(Year other) Checks if this year is after the specified year.
|
isBefore | back to summary |
---|---|
public boolean isBefore(Year other) Checks if this year is before the specified year.
|
isLeap | back to summary |
---|---|
public static boolean isLeap(long year) 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.
|
isLeap | back to summary |
---|---|
public boolean isLeap() 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 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.
If false, then calling the
If the unit is a
ChronoUnit instances will return false.
If the unit is not a
|
isValidMonthDay | back to summary |
---|---|
public boolean isValidMonthDay(MonthDay monthDay) Checks if the month-day is valid for this year. This method checks whether this year and the input month and day form a valid date.
|
length | back to summary |
---|---|
public int length() Gets the length of this year in days.
|
minus | back to summary |
---|---|
public Year minus(TemporalAmount amountToSubtract) Overrides default java. Returns a copy of this year 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 Year minus(long amountToSubtract, TemporalUnit unit) Overrides default java. Returns a copy of this year with the specified amount subtracted.
This returns a
This method is equivalent to This instance is immutable and unaffected by this method call.
|
minusYears | back to summary |
---|---|
public Year minusYears(long yearsToSubtract) Returns a copy of this This instance is immutable and unaffected by this method call.
|
now | back to summary |
---|---|
public static Year now() Obtains the current year 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 Year now(ZoneId zone) Obtains the current year 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 Year now(Clock clock) Obtains the current year from the specified clock.
This will query the specified clock to obtain the current year.
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 Year of(int isoYear) Obtains an instance of This method accepts a year value from the proleptic ISO calendar system.
The year 2AD/CE is represented by 2.
|
parse | back to summary |
---|---|
public static Year parse(CharSequence text) Obtains an instance of The string must represent a valid year.
|
parse | back to summary |
---|---|
public static Year parse(CharSequence text, DateTimeFormatter formatter) Obtains an instance of The text is parsed using the formatter, returning a year.
|
plus | back to summary |
---|---|
public Year plus(TemporalAmount amountToAdd) Overrides default java. Returns a copy of this year 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 Year plus(long amountToAdd, TemporalUnit unit) Implements java. Returns a copy of this year 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.
|
plusYears | back to summary |
---|---|
public Year 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 using the specified query.
This queries this year 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 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 Year 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 as a |
until | back to summary |
---|---|
public long until(Temporal endExclusive, TemporalUnit unit) Implements java. Calculates the amount of time until another year 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 years. For example, the amount in decades between 2012 and 2031 will only be one decade as it is one year 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, YEARS); amount = YEARS.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 |
---|---|
public Year with(TemporalAdjuster adjuster) Overrides default java. Returns an adjusted copy of this year.
This returns a
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 Year with(TemporalField field, long newValue) Implements java. Returns a copy of this year 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.
|
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. |