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
TenorimplementsTemporalAmountallowing 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 TenorTENOR_10MA tenor of 10 months.static TenorTENOR_10YA tenor of 10 years.static TenorTENOR_11MA tenor of 11 months.static TenorTENOR_11YA tenor of 11 years.static TenorTENOR_12MA tenor of 12 months.static TenorTENOR_12YA tenor of 12 years.static TenorTENOR_13WA tenor of 13 weeks.static TenorTENOR_13YA tenor of 13 years.static TenorTENOR_14YA tenor of 14 years.static TenorTENOR_15MA tenor of 15 months.static TenorTENOR_15YA tenor of 15 years.static TenorTENOR_18MA tenor of 18 months.static TenorTENOR_1DA tenor of one day.static TenorTENOR_1MA tenor of 1 month.static TenorTENOR_1WA tenor of 1 week.static TenorTENOR_1YA tenor of 1 year.static TenorTENOR_20YA tenor of 20 years.static TenorTENOR_21MA tenor of 21 months.static TenorTENOR_25YA tenor of 25 years.static TenorTENOR_26WA tenor of 26 weeks.static TenorTENOR_2DA tenor of two days.static TenorTENOR_2MA tenor of 2 months.static TenorTENOR_2WA tenor of 2 weeks.static TenorTENOR_2YA tenor of 2 years.static TenorTENOR_30YA tenor of 30 years.static TenorTENOR_35YA tenor of 35 years.static TenorTENOR_3DA tenor of three days.static TenorTENOR_3MA tenor of 3 months.static TenorTENOR_3WA tenor of 3 weeks.static TenorTENOR_3YA tenor of 3 years.static TenorTENOR_40YA tenor of 40 years.static TenorTENOR_45YA tenor of 45 years.static TenorTENOR_4MA tenor of 4 months.static TenorTENOR_4WA tenor of 4 weeks.static TenorTENOR_4YA tenor of 4 years.static TenorTENOR_50YA tenor of 50 years.static TenorTENOR_52WA tenor of 52 weeks.static TenorTENOR_5MA tenor of 5 months.static TenorTENOR_5YA tenor of 5 years.static TenorTENOR_6MA tenor of 6 months.static TenorTENOR_6WA tenor of 6 weeks.static TenorTENOR_6YA tenor of 6 years.static TenorTENOR_7MA tenor of 7 months.static TenorTENOR_7YA tenor of 7 years.static TenorTENOR_8MA tenor of 8 months.static TenorTENOR_8YA tenor of 8 years.static TenorTENOR_9MA tenor of 9 months.static TenorTENOR_9YA tenor of 9 years.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TemporaladdTo(Temporal temporal)Adds this tenor to the specified date.intcompareTo(Tenor other)Compares this tenor to another tenor.booleanequals(Object obj)Checks if this tenor equals another tenor.longget(TemporalUnit unit)Gets the value of the specified unit.PeriodgetPeriod()Gets the underlying period of the tenor.List<TemporalUnit>getUnits()Gets the units supported by a tenor.inthashCode()Returns a suitable hash code for the tenor.booleanisMonthBased()Checks if the tenor is month-based.booleanisWeekBased()Checks if the tenor is week-based.Tenornormalized()Normalizes the months and years of this tenor.static Tenorof(Period period)Obtains an instance from aPeriod.static TenorofDays(int days)Obtains an instance backed by a period of days.static TenorofMonths(int months)Obtains an instance backed by a period of months.static TenorofWeeks(int weeks)Obtains an instance backed by a period of weeks.static TenorofYears(int years)Obtains an instance backed by a period of years.static Tenorparse(String toParse)Parses a formatted string representing the tenor.TemporalsubtractFrom(Temporal temporal)Subtracts this tenor from the specified date.StringtoString()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:
getin 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:
getUnitsin 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:
addToin 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:
subtractFromin 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:
compareToin 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.
-
-