public final class Years extends PeriodField implements java.lang.Comparable<Years>, java.io.Serializable
Years is an immutable period that can only store years. It is a type-safe way of representing a number of years in an application.
Static factory methods allow you to construct instances. The number of years may be queried using getYears(). Basic mathematical operations are provided - plus(), minus(), multipliedBy(), dividedBy() and negated(), all of which return a new instance
Years is immutable and thread-safe.
Modifier and Type | Field and Description |
---|---|
static Years |
ZERO
A constant for zero years.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Years otherYears)
Compares the number of years in this instance to another instance.
|
Years |
dividedBy(int divisor)
Returns a new instance with the years divided by the specified divisor.
|
int |
getAmount()
Gets the number of years held in this period.
|
PeriodUnit |
getUnit()
Gets the unit defining the amount of time.
|
boolean |
isGreaterThan(Years otherYears)
Is the number of years in this instance greater than that in
another instance.
|
boolean |
isLessThan(Years otherYears)
Is the number of years in this instance less than that in
another instance.
|
Years |
minus(int years)
Returns a new instance with the specified number of years taken away.
|
Years |
minus(Years years)
Returns a new instance with the specified number of years taken away.
|
Years |
multipliedBy(int scalar)
Returns a new instance with the years multiplied by the specified scalar.
|
Years |
negated()
Returns a new instance with the years value negated.
|
static Years |
of(int years)
Obtains an instance of
Years . |
Years |
plus(int years)
Returns a new instance with the specified number of years added.
|
Years |
plus(Years years)
Returns a new instance with the specified number of years added.
|
java.lang.String |
toString()
Returns a string representation of the number of years.
|
Years |
withAmount(int amount)
Returns a new instance of the subclass with a different number of years.
|
equals, hashCode, toPeriodFields
public static final Years ZERO
public static Years of(int years)
Years
.years
- the number of years the instance will representpublic int getAmount()
getAmount
in class PeriodField
public Years withAmount(int amount)
withAmount
in class PeriodField
amount
- the number of years to set in the new instance, may be negativepublic PeriodUnit getUnit()
getUnit
in class PeriodField
public Years plus(int years)
This instance is immutable and unaffected by this method call.
plus
in class PeriodField
years
- the amount of years to add, may be negativejava.lang.ArithmeticException
- if the result overflows an intpublic Years plus(Years years)
This instance is immutable and unaffected by this method call.
years
- the amount of years to add, may be negative, not nulljava.lang.NullPointerException
- if the years to add is nulljava.lang.ArithmeticException
- if the result overflows an intpublic Years minus(int years)
This instance is immutable and unaffected by this method call.
minus
in class PeriodField
years
- the amount of years to take away, may be negativejava.lang.ArithmeticException
- if the result overflows an intpublic Years minus(Years years)
This instance is immutable and unaffected by this method call.
years
- the amount of years to take away, may be negative, not nulljava.lang.NullPointerException
- if the years to add is nulljava.lang.ArithmeticException
- if the result overflows an intpublic Years multipliedBy(int scalar)
This instance is immutable and unaffected by this method call.
multipliedBy
in class PeriodField
scalar
- the amount to multiply by, may be negativejava.lang.ArithmeticException
- if the result overflows an intpublic Years dividedBy(int divisor)
This instance is immutable and unaffected by this method call.
dividedBy
in class PeriodField
divisor
- the amount to divide by, may be negativejava.lang.ArithmeticException
- if the divisor is zeropublic Years negated()
negated
in class PeriodField
java.lang.ArithmeticException
- if the result overflows an intpublic int compareTo(Years otherYears)
compareTo
in interface java.lang.Comparable<Years>
otherYears
- the other number of years, not nulljava.lang.NullPointerException
- if otherYears is nullpublic boolean isGreaterThan(Years otherYears)
otherYears
- the other number of years, not nulljava.lang.NullPointerException
- if otherYears is nullpublic boolean isLessThan(Years otherYears)
otherYears
- the other number of years, not nulljava.lang.NullPointerException
- if otherYears is nullpublic java.lang.String toString()
toString
in class PeriodField