Class Tenor
- java.lang.Object
-
- com.opengamma.strata.basics.date.Tenor
-
- All Implemented Interfaces:
Serializable
,Comparable<Tenor>
,TemporalAmount
public final class Tenor extends Object implements Comparable<Tenor>, TemporalAmount, Serializable
A tenor indicating how long it will take for a financial instrument to reach maturity.A tenor is allowed to be any non-negative non-zero period of days, weeks, month or years. This class provides constants for common tenors which are best used by static import.
Each tenor is based on a
Period
. The months and years of the period are not normalized, thus it is possible to have a tenor of 12 months and a different one of 1 year. When used, standard date addition rules apply, thus there is no difference between them. Callnormalized()
to apply normalization.Usage
Tenor
implementsTemporalAmount
allowing it to be directly added to a date:LocalDate later = baseDate.plus(tenor);
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static Tenor
TENOR_10M
A tenor of 10 months.static Tenor
TENOR_10Y
A tenor of 10 years.static Tenor
TENOR_11M
A tenor of 11 months.static Tenor
TENOR_11Y
A tenor of 11 years.static Tenor
TENOR_12M
A tenor of 12 months.static Tenor
TENOR_12Y
A tenor of 12 years.static Tenor
TENOR_13W
A tenor of 13 weeks.static Tenor
TENOR_13Y
A tenor of 13 years.static Tenor
TENOR_14Y
A tenor of 14 years.static Tenor
TENOR_15M
A tenor of 15 months.static Tenor
TENOR_15Y
A tenor of 15 years.static Tenor
TENOR_18M
A tenor of 18 months.static Tenor
TENOR_1D
A tenor of one day.static Tenor
TENOR_1M
A tenor of 1 month.static Tenor
TENOR_1W
A tenor of 1 week.static Tenor
TENOR_1Y
A tenor of 1 year.static Tenor
TENOR_20Y
A tenor of 20 years.static Tenor
TENOR_21M
A tenor of 21 months.static Tenor
TENOR_25Y
A tenor of 25 years.static Tenor
TENOR_26W
A tenor of 26 weeks.static Tenor
TENOR_2D
A tenor of two days.static Tenor
TENOR_2M
A tenor of 2 months.static Tenor
TENOR_2W
A tenor of 2 weeks.static Tenor
TENOR_2Y
A tenor of 2 years.static Tenor
TENOR_30Y
A tenor of 30 years.static Tenor
TENOR_35Y
A tenor of 35 years.static Tenor
TENOR_3D
A tenor of three days.static Tenor
TENOR_3M
A tenor of 3 months.static Tenor
TENOR_3W
A tenor of 3 weeks.static Tenor
TENOR_3Y
A tenor of 3 years.static Tenor
TENOR_40Y
A tenor of 40 years.static Tenor
TENOR_45Y
A tenor of 45 years.static Tenor
TENOR_4M
A tenor of 4 months.static Tenor
TENOR_4W
A tenor of 4 weeks.static Tenor
TENOR_4Y
A tenor of 4 years.static Tenor
TENOR_50Y
A tenor of 50 years.static Tenor
TENOR_52W
A tenor of 52 weeks.static Tenor
TENOR_5M
A tenor of 5 months.static Tenor
TENOR_5Y
A tenor of 5 years.static Tenor
TENOR_6M
A tenor of 6 months.static Tenor
TENOR_6W
A tenor of 6 weeks.static Tenor
TENOR_6Y
A tenor of 6 years.static Tenor
TENOR_7M
A tenor of 7 months.static Tenor
TENOR_7Y
A tenor of 7 years.static Tenor
TENOR_8M
A tenor of 8 months.static Tenor
TENOR_8Y
A tenor of 8 years.static Tenor
TENOR_9M
A tenor of 9 months.static Tenor
TENOR_9Y
A tenor of 9 years.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Temporal
addTo(Temporal temporal)
Adds this tenor to the specified date.int
compareTo(Tenor other)
Compares this tenor to another tenor.boolean
equals(Object obj)
Checks if this tenor equals another tenor.long
get(TemporalUnit unit)
Gets the value of the specified unit.Period
getPeriod()
Gets the underlying period of the tenor.List<TemporalUnit>
getUnits()
Gets the units supported by a tenor.int
hashCode()
Returns a suitable hash code for the tenor.boolean
isMonthBased()
Checks if the tenor is month-based.boolean
isWeekBased()
Checks if the tenor is week-based.Tenor
normalized()
Normalizes the months and years of this tenor.static Tenor
of(Period period)
Obtains an instance from aPeriod
.static Tenor
ofDays(int days)
Obtains an instance backed by a period of days.static Tenor
ofMonths(int months)
Obtains an instance backed by a period of months.static Tenor
ofWeeks(int weeks)
Obtains an instance backed by a period of weeks.static Tenor
ofYears(int years)
Obtains an instance backed by a period of years.static Tenor
parse(String toParse)
Parses a formatted string representing the tenor.Temporal
subtractFrom(Temporal temporal)
Subtracts this tenor from the specified date.String
toString()
Returns a formatted string representing the tenor.
-
-
-
Field Detail
-
TENOR_1D
public static final Tenor TENOR_1D
A tenor of one day.
-
TENOR_2D
public static final Tenor TENOR_2D
A tenor of two days.
-
TENOR_3D
public static final Tenor TENOR_3D
A tenor of three days.
-
TENOR_1W
public static final Tenor TENOR_1W
A tenor of 1 week.
-
TENOR_2W
public static final Tenor TENOR_2W
A tenor of 2 weeks.
-
TENOR_3W
public static final Tenor TENOR_3W
A tenor of 3 weeks.
-
TENOR_4W
public static final Tenor TENOR_4W
A tenor of 4 weeks.
-
TENOR_6W
public static final Tenor TENOR_6W
A tenor of 6 weeks.
-
TENOR_13W
public static final Tenor TENOR_13W
A tenor of 13 weeks.
-
TENOR_26W
public static final Tenor TENOR_26W
A tenor of 26 weeks.
-
TENOR_52W
public static final Tenor TENOR_52W
A tenor of 52 weeks.
-
TENOR_1M
public static final Tenor TENOR_1M
A tenor of 1 month.
-
TENOR_2M
public static final Tenor TENOR_2M
A tenor of 2 months.
-
TENOR_3M
public static final Tenor TENOR_3M
A tenor of 3 months.
-
TENOR_4M
public static final Tenor TENOR_4M
A tenor of 4 months.
-
TENOR_5M
public static final Tenor TENOR_5M
A tenor of 5 months.
-
TENOR_6M
public static final Tenor TENOR_6M
A tenor of 6 months.
-
TENOR_7M
public static final Tenor TENOR_7M
A tenor of 7 months.
-
TENOR_8M
public static final Tenor TENOR_8M
A tenor of 8 months.
-
TENOR_9M
public static final Tenor TENOR_9M
A tenor of 9 months.
-
TENOR_10M
public static final Tenor TENOR_10M
A tenor of 10 months.
-
TENOR_11M
public static final Tenor TENOR_11M
A tenor of 11 months.
-
TENOR_12M
public static final Tenor TENOR_12M
A tenor of 12 months.
-
TENOR_15M
public static final Tenor TENOR_15M
A tenor of 15 months.
-
TENOR_18M
public static final Tenor TENOR_18M
A tenor of 18 months.
-
TENOR_21M
public static final Tenor TENOR_21M
A tenor of 21 months.
-
TENOR_1Y
public static final Tenor TENOR_1Y
A tenor of 1 year.
-
TENOR_2Y
public static final Tenor TENOR_2Y
A tenor of 2 years.
-
TENOR_3Y
public static final Tenor TENOR_3Y
A tenor of 3 years.
-
TENOR_4Y
public static final Tenor TENOR_4Y
A tenor of 4 years.
-
TENOR_5Y
public static final Tenor TENOR_5Y
A tenor of 5 years.
-
TENOR_6Y
public static final Tenor TENOR_6Y
A tenor of 6 years.
-
TENOR_7Y
public static final Tenor TENOR_7Y
A tenor of 7 years.
-
TENOR_8Y
public static final Tenor TENOR_8Y
A tenor of 8 years.
-
TENOR_9Y
public static final Tenor TENOR_9Y
A tenor of 9 years.
-
TENOR_10Y
public static final Tenor TENOR_10Y
A tenor of 10 years.
-
TENOR_11Y
public static final Tenor TENOR_11Y
A tenor of 11 years.
-
TENOR_12Y
public static final Tenor TENOR_12Y
A tenor of 12 years.
-
TENOR_13Y
public static final Tenor TENOR_13Y
A tenor of 13 years.
-
TENOR_14Y
public static final Tenor TENOR_14Y
A tenor of 14 years.
-
TENOR_15Y
public static final Tenor TENOR_15Y
A tenor of 15 years.
-
TENOR_20Y
public static final Tenor TENOR_20Y
A tenor of 20 years.
-
TENOR_25Y
public static final Tenor TENOR_25Y
A tenor of 25 years.
-
TENOR_30Y
public static final Tenor TENOR_30Y
A tenor of 30 years.
-
TENOR_35Y
public static final Tenor TENOR_35Y
A tenor of 35 years.
-
TENOR_40Y
public static final Tenor TENOR_40Y
A tenor of 40 years.
-
TENOR_45Y
public static final Tenor TENOR_45Y
A tenor of 45 years.
-
TENOR_50Y
public static final Tenor TENOR_50Y
A tenor of 50 years.
-
-
Method Detail
-
of
public static Tenor of(Period period)
Obtains an instance from aPeriod
.The period normally consists of either days and weeks, or months and years. It must also be positive and non-zero.
If the number of days is an exact multiple of 7 it will be converted to weeks. Months are not normalized into years.
- Parameters:
period
- the period to convert to a tenor- Returns:
- the tenor
- Throws:
IllegalArgumentException
- if the period is negative or zero
-
ofDays
public static Tenor ofDays(int days)
Obtains an instance backed by a period of days.If the number of days is an exact multiple of 7 it will be converted to weeks.
- Parameters:
days
- the number of days- Returns:
- the tenor
- Throws:
IllegalArgumentException
- if days is negative or zero
-
ofWeeks
public static Tenor ofWeeks(int weeks)
Obtains an instance backed by a period of weeks.- Parameters:
weeks
- the number of weeks- Returns:
- the tenor
- Throws:
IllegalArgumentException
- if weeks is negative or zero
-
ofMonths
public static Tenor ofMonths(int months)
Obtains an instance backed by a period of months.Months are not normalized into years.
- Parameters:
months
- the number of months- Returns:
- the tenor
- Throws:
IllegalArgumentException
- if months is negative or zero
-
ofYears
public static Tenor ofYears(int years)
Obtains an instance backed by a period of years.- Parameters:
years
- the number of years- Returns:
- the tenor
- Throws:
IllegalArgumentException
- if years is negative or zero
-
parse
public static Tenor parse(String toParse)
Parses a formatted string representing the tenor.The format can either be based on ISO-8601, such as 'P3M' or without the 'P' prefix e.g. '2W'.
- Parameters:
toParse
- the string representing the tenor- Returns:
- the tenor
- Throws:
IllegalArgumentException
- if the tenor cannot be parsed
-
getPeriod
public Period getPeriod()
Gets the underlying period of the tenor.- Returns:
- the period
-
normalized
public Tenor normalized()
Normalizes the months and years of this tenor.This method returns a normalized tenor of an equivalent length. If the period is exactly 1 year then the result will be expressed as 12 months. Otherwise, the result will be expressed using
Period.normalized()
.- Returns:
- the normalized tenor
-
isWeekBased
public boolean isWeekBased()
Checks if the tenor is week-based.A week-based tenor consists of an integral number of weeks. There must be no day, month or year element.
- Returns:
- true if this is week-based
-
isMonthBased
public boolean isMonthBased()
Checks if the tenor is month-based.A month-based tenor consists of an integral number of months. Any year-based tenor is also counted as month-based. There must be no day or week element.
- Returns:
- true if this is month-based
-
get
public long get(TemporalUnit unit)
Gets the value of the specified unit.This will return a value for the years, months and days units. Note that weeks are not included. All other units throw an exception.
This method implements
TemporalAmount
. It is not intended to be called directly.- Specified by:
get
in interfaceTemporalAmount
- Parameters:
unit
- the unit to query- Returns:
- the value of the unit
- Throws:
UnsupportedTemporalTypeException
- if the unit is not supported
-
getUnits
public List<TemporalUnit> getUnits()
Gets the units supported by a tenor.This returns a list containing years, months and days. Note that weeks are not included.
This method implements
TemporalAmount
. It is not intended to be called directly.- Specified by:
getUnits
in interfaceTemporalAmount
- Returns:
- a list containing the years, months and days units
-
addTo
public Temporal addTo(Temporal temporal)
Adds this tenor to the specified date.This method implements
TemporalAmount
. It is not intended to be called directly. UseLocalDate.plus(TemporalAmount)
instead.- Specified by:
addTo
in interfaceTemporalAmount
- Parameters:
temporal
- the temporal object to add to- Returns:
- the result with this tenor added
- Throws:
DateTimeException
- if unable to addArithmeticException
- if numeric overflow occurs
-
subtractFrom
public Temporal subtractFrom(Temporal temporal)
Subtracts this tenor from the specified date.This method implements
TemporalAmount
. It is not intended to be called directly. UseLocalDate.minus(TemporalAmount)
instead.- Specified by:
subtractFrom
in interfaceTemporalAmount
- Parameters:
temporal
- the temporal object to subtract from- Returns:
- the result with this tenor subtracted
- Throws:
DateTimeException
- if unable to subtractArithmeticException
- if numeric overflow occurs
-
compareTo
public int compareTo(Tenor other)
Compares this tenor to another tenor.Comparing tenors is a hard problem in general, but for commonly used tenors the outcome is as expected. If the two tenors are both based on days, then comparison is easy. If the two tenors are both based on months/years, then comparison is easy. Otherwise, months are converted to days to form an estimated length in days which is compared. The conversion from months to days divides by 12 and then multiplies by 365.2425. As a result, this method is compatible with
equals(Object)
for tenors less than 400 years.The resulting order places:
- a 1 month tenor between 30 and 31 days
- a 2 month tenor between 60 and 61 days
- a 3 month tenor between 91 and 92 days
- a 6 month tenor between 182 and 183 days
- a 1 year tenor between 365 and 366 days
- a 4 year tenor between 1460 and 1461 days
- Specified by:
compareTo
in interfaceComparable<Tenor>
- Parameters:
other
- the other tenor- Returns:
- negative if this is less than the other, zero if equal and positive if greater
-
equals
public boolean equals(Object obj)
Checks if this tenor equals another tenor.The comparison checks the tenor period.
-
hashCode
public int hashCode()
Returns a suitable hash code for the tenor.
-
-