public final class Tenor extends Object implements Comparable<Tenor>, TemporalAmount, Serializable
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.
Call normalized()
to apply normalization.
Tenor
implements TemporalAmount
allowing it to be directly added to a date:
LocalDate later = baseDate.plus(tenor);
Modifier and Type | Field and 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.
|
Modifier and Type | Method and 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 a
Period . |
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.
|
public static final Tenor TENOR_1D
public static final Tenor TENOR_2D
public static final Tenor TENOR_3D
public static final Tenor TENOR_1W
public static final Tenor TENOR_2W
public static final Tenor TENOR_3W
public static final Tenor TENOR_4W
public static final Tenor TENOR_6W
public static final Tenor TENOR_13W
public static final Tenor TENOR_26W
public static final Tenor TENOR_52W
public static final Tenor TENOR_1M
public static final Tenor TENOR_2M
public static final Tenor TENOR_3M
public static final Tenor TENOR_4M
public static final Tenor TENOR_5M
public static final Tenor TENOR_6M
public static final Tenor TENOR_7M
public static final Tenor TENOR_8M
public static final Tenor TENOR_9M
public static final Tenor TENOR_10M
public static final Tenor TENOR_11M
public static final Tenor TENOR_12M
public static final Tenor TENOR_15M
public static final Tenor TENOR_18M
public static final Tenor TENOR_21M
public static final Tenor TENOR_1Y
public static final Tenor TENOR_2Y
public static final Tenor TENOR_3Y
public static final Tenor TENOR_4Y
public static final Tenor TENOR_5Y
public static final Tenor TENOR_6Y
public static final Tenor TENOR_7Y
public static final Tenor TENOR_8Y
public static final Tenor TENOR_9Y
public static final Tenor TENOR_10Y
public static final Tenor TENOR_11Y
public static final Tenor TENOR_12Y
public static final Tenor TENOR_13Y
public static final Tenor TENOR_14Y
public static final Tenor TENOR_15Y
public static final Tenor TENOR_20Y
public static final Tenor TENOR_25Y
public static final Tenor TENOR_30Y
public static final Tenor TENOR_35Y
public static final Tenor TENOR_40Y
public static final Tenor TENOR_45Y
public static final Tenor TENOR_50Y
public static Tenor of(Period period)
Period
.
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.
period
- the period to convert to a tenorIllegalArgumentException
- if the period is negative or zeropublic static Tenor ofDays(int days)
If the number of days is an exact multiple of 7 it will be converted to weeks.
days
- the number of daysIllegalArgumentException
- if days is negative or zeropublic static Tenor ofWeeks(int weeks)
weeks
- the number of weeksIllegalArgumentException
- if weeks is negative or zeropublic static Tenor ofMonths(int months)
Months are not normalized into years.
months
- the number of monthsIllegalArgumentException
- if months is negative or zeropublic static Tenor ofYears(int years)
years
- the number of yearsIllegalArgumentException
- if years is negative or zeropublic static Tenor parse(String toParse)
The format can either be based on ISO-8601, such as 'P3M' or without the 'P' prefix e.g. '2W'.
toParse
- the string representing the tenorIllegalArgumentException
- if the tenor cannot be parsedpublic Period getPeriod()
public Tenor normalized()
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()
.
public boolean isWeekBased()
A week-based tenor consists of an integral number of weeks. There must be no day, month or year element.
public boolean isMonthBased()
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.
public long get(TemporalUnit 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.
get
in interface TemporalAmount
unit
- the unit to queryUnsupportedTemporalTypeException
- if the unit is not supportedpublic List<TemporalUnit> getUnits()
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.
getUnits
in interface TemporalAmount
public Temporal addTo(Temporal temporal)
This method implements TemporalAmount
.
It is not intended to be called directly.
Use LocalDate.plus(TemporalAmount)
instead.
addTo
in interface TemporalAmount
temporal
- the temporal object to add toDateTimeException
- if unable to addArithmeticException
- if numeric overflow occurspublic Temporal subtractFrom(Temporal temporal)
This method implements TemporalAmount
.
It is not intended to be called directly.
Use LocalDate.minus(TemporalAmount)
instead.
subtractFrom
in interface TemporalAmount
temporal
- the temporal object to subtract fromDateTimeException
- if unable to subtractArithmeticException
- if numeric overflow occurspublic int compareTo(Tenor other)
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.25.
The resulting order places:
compareTo
in interface Comparable<Tenor>
other
- the other tenorpublic boolean equals(Object obj)
The comparison checks the tenor period.
public int hashCode()
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.