public final class Years extends Object implements TemporalAmount, Comparable<Years>, Serializable
This class models a quantity or amount of time in terms of years. It is a typesafe way of representing a number of years in an application.
The model is of a directed amount, meaning that the amount may be negative.
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  Field and Description 

static Years 
ONE
A constant for one year.

static Years 
ZERO
A constant for zero years.

Modifier and Type  Method and Description 

Years 
abs()
Returns a copy of this duration with a positive length.

Temporal 
addTo(Temporal temporal)
Adds this amount to the specified temporal object.

static Years 
between(Temporal startDateInclusive,
Temporal endDateExclusive)
Obtains a
Years consisting of the number of years between two dates. 
int 
compareTo(Years otherAmount)
Compares this amount to the specified
Years . 
Years 
dividedBy(int divisor)
Returns an instance with the amount divided by the specified divisor.

boolean 
equals(Object otherAmount)
Checks if this amount is equal to the specified
Years . 
static Years 
from(TemporalAmount amount)
Obtains an instance of
Years from a temporal amount. 
long 
get(TemporalUnit unit)
Gets the value of the requested unit.

int 
getAmount()
Gets the number of years in this amount.

List<TemporalUnit> 
getUnits()
Gets the set of units supported by this amount.

int 
hashCode()
A hash code for this amount.

Years 
minus(int years)
Returns a copy of this amount with the specified number of years subtracted.

Years 
minus(TemporalAmount amountToAdd)
Returns a copy of this amount with the specified amount subtracted.

Years 
multipliedBy(int scalar)
Returns an instance with the amount multiplied by the specified scalar.

Years 
negated()
Returns an instance with the amount negated.

static Years 
of(int years)
Obtains a
Years representing a number of years. 
static Years 
parse(CharSequence text)
Obtains a
Years from a text string such as PnY . 
Years 
plus(int years)
Returns a copy of this amount with the specified number of years added.

Years 
plus(TemporalAmount amountToAdd)
Returns a copy of this amount with the specified amount added.

Temporal 
subtractFrom(Temporal temporal)
Subtracts this amount from the specified temporal object.

Period 
toPeriod()
Gets the number of years as a
Period . 
String 
toString()
Returns a string representation of the number of years.

public static final Years ZERO
public static final Years ONE
public static Years of(int years)
Years
representing a number of years.
The resulting amount will have the specified years.
years
 the number of years, positive or negativepublic static Years from(TemporalAmount amount)
Years
from a temporal amount.
This obtains an instance based on the specified amount.
A TemporalAmount
represents an amount of time, which may be
datebased or timebased, which this factory extracts to a Years
.
The result is calculated by looping around each unit in the specified amount.
Each amount is converted to years using Temporals.convertAmount(long, java.time.temporal.TemporalUnit, java.time.temporal.TemporalUnit)
.
If the conversion yields a remainder, an exception is thrown.
If the amount is zero, the unit is ignored.
For example, "12 months" can be converted to years but "11 months" cannot.
amount
 the temporal amount to convert, not nullDateTimeException
 if unable to convert to a Years
ArithmeticException
 if numeric overflow occurspublic static Years parse(CharSequence text)
Years
from a text string such as PnY
.
This will parse the string produced by toString()
which is
based on the ISO8601 period formats PnY
.
The string starts with an optional sign, denoted by the ASCII negative or positive symbol. If negative, the whole amount is negated. The ASCII letter "P" is next in upper or lower case. The ASCII integer amount is next, which may be negative. The ASCII letter "Y" is next in upper or lower case.
The leading plus/minus sign, and negative values for years are not part of the ISO8601 standard.
For example, the following are valid inputs:
"P2Y"  Years.of(2) "P2Y"  Years.of(2) "P2Y"  Years.of(2) "P2Y"  Years.of(2)
text
 the text to parse, not nullDateTimeParseException
 if the text cannot be parsed to a periodpublic static Years between(Temporal startDateInclusive, Temporal endDateExclusive)
Years
consisting of the number of years between two dates.
The start date is included, but the end date is not. The result of this method can be negative if the end is before the start.
startDateInclusive
 the start date, inclusive, not nullendDateExclusive
 the end date, exclusive, not nullpublic long get(TemporalUnit unit)
This returns a value for the supported unit  YEARS
.
All other units throw an exception.
get
in interface TemporalAmount
unit
 the TemporalUnit
for which to return the valueUnsupportedTemporalTypeException
 if the unit is not supportedpublic List<TemporalUnit> getUnits()
The single supported unit is YEARS
.
This set can be used in conjunction with get(TemporalUnit)
to access the entire state of the amount.
getUnits
in interface TemporalAmount
public int getAmount()
public Years plus(TemporalAmount amountToAdd)
The parameter is converted using from(TemporalAmount)
.
This instance is immutable and unaffected by this method call.
amountToAdd
 the amount to add, not nullYears
based on this instance with the requested amount added, not nullDateTimeException
 if the specified amount contains an invalid unitArithmeticException
 if numeric overflow occurspublic Years plus(int years)
This instance is immutable and unaffected by this method call.
years
 the amount of years to add, may be negativeYears
based on this instance with the requested amount added, not nullArithmeticException
 if the result overflows an intpublic Years minus(TemporalAmount amountToAdd)
The parameter is converted using from(TemporalAmount)
.
This instance is immutable and unaffected by this method call.
amountToAdd
 the amount to add, not nullYears
based on this instance with the requested amount subtracted, not nullDateTimeException
 if the specified amount contains an invalid unitArithmeticException
 if numeric overflow occurspublic Years minus(int years)
This instance is immutable and unaffected by this method call.
years
 the amount of years to add, may be negativeYears
based on this instance with the requested amount subtracted, not nullArithmeticException
 if the result overflows an intpublic Years multipliedBy(int scalar)
This instance is immutable and unaffected by this method call.
scalar
 the scalar to multiply by, not nullArithmeticException
 if numeric overflow occurspublic Years dividedBy(int divisor)
The calculation uses integer division, thus 3 divided by 2 is 1.
This instance is immutable and unaffected by this method call.
divisor
 the amount to divide by, may be negativeArithmeticException
 if the divisor is zeropublic Years negated()
This instance is immutable and unaffected by this method call.
ArithmeticException
 if numeric overflow occurs, which only happens if
the amount is Long.MIN_VALUE
public Years abs()
This method returns a positive duration by effectively removing the sign from any negative total length.
This instance is immutable and unaffected by this method call.
ArithmeticException
 if numeric overflow occurs, which only happens if
the amount is Long.MIN_VALUE
public Period toPeriod()
Period
.
This returns a period with the same number of years.
public Temporal addTo(Temporal temporal)
This returns a temporal object of the same observable type as the input with this amount added.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.plus(TemporalAmount)
.
// these two lines are equivalent, but the second approach is recommended dateTime = thisAmount.addTo(dateTime); dateTime = dateTime.plus(thisAmount);
Only nonzero amounts will be added.
This instance is immutable and unaffected by this method call.
addTo
in interface TemporalAmount
temporal
 the temporal object to adjust, not nullDateTimeException
 if unable to addUnsupportedTemporalTypeException
 if the YEARS unit is not supportedArithmeticException
 if numeric overflow occurspublic Temporal subtractFrom(Temporal temporal)
This returns a temporal object of the same observable type as the input with this amount subtracted.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.minus(TemporalAmount)
.
// these two lines are equivalent, but the second approach is recommended dateTime = thisAmount.subtractFrom(dateTime); dateTime = dateTime.minus(thisAmount);
Only nonzero amounts will be subtracted.
This instance is immutable and unaffected by this method call.
subtractFrom
in interface TemporalAmount
temporal
 the temporal object to adjust, not nullDateTimeException
 if unable to subtractUnsupportedTemporalTypeException
 if the YEARS unit is not supportedArithmeticException
 if numeric overflow occurspublic int compareTo(Years otherAmount)
Years
.
The comparison is based on the total length of the amounts.
It is "consistent with equals", as defined by Comparable
.
compareTo
in interface Comparable<Years>
otherAmount
 the other amount, not nullpublic boolean equals(Object otherAmount)
Years
.
The comparison is based on the total length of the durations.
public int hashCode()
Copyright © 2010–2017 ThreeTen.org. All rights reserved.