YearQuarter
Extends:
A year-quarter in the ISO-8601 calendar system, such as 2007-Q2
.
YearQuarter
is an immutable date-time object that represents the combination
of a year and quarter. Any field that can be derived from a year and quarter can be obtained.
A quarter is defined by Quarter and Month.firstMonthOfQuarter - Q1, Q2, Q3 and Q4.
Q1 is January to March, Q2 is April to June, Q3 is July to September and Q4 is October to December.
This class does not store or represent a day, time or time-zone.
For example, the value '2nd quarter 2007' can be stored in a YearQuarter
.
The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable. Note that the ISO-8601 standard does not define or refer to quarters.
Static Method Summary
Static Public Methods | ||
public static |
from(temporal: TemporalAccessor): YearQuarter Obtains an instance of |
|
public static |
now(zoneIdOrClock: ZoneId | Clock): YearQuarter function overloading for YearQuarter.now
|
|
public static |
of(year: Year | number, quarter: Quarter | number): YearQuarter function overloading for YearQuarter.of
|
|
public static |
parse(text: string, formatter: DateTimeFormatter): YearQuarter Obtains an instance of |
Static Protected Methods | ||
protected static |
_now0(): YearQuarter Obtains the current year-quarter from the system clock in the default time-zone. |
|
protected static |
_nowZoneId(zone: ZoneId): YearQuarter Obtains the current year-quarter from the system clock in the specified time-zone. |
|
protected static |
_ofIntInt(year: number, quarter: number): YearQuarter Obtains an instance of |
|
protected static |
_ofIntQuarter(year: number, quarter: Quarter): YearQuarter Obtains an instance of |
|
protected static |
_ofYearInt(year: Year, quarter: number): YearQuarter Obtains an instance of |
|
protected static |
_ofYearQuarter(year: Year, quarter: Quarter): YearQuarter Obtains an instance of |
Method Summary
Public Methods | ||
public |
adjustInto(temporal: Temporal): Temporal Adjusts the specified temporal object to have this year-quarter. |
|
public |
Combines this year-quarter with a day-of-quarter to create a |
|
public |
Returns a |
|
public |
compareTo(other: YearQuarter): number Compares this year-quarter to another |
|
public |
equals(obj: *): boolean Checks if this year-quarter is equal to another year-quarter. |
|
public |
format(formatter: DateTimeFormatter): string Formats this year-quarter using the specified formatter. |
|
public |
get(field: TemporalField): number Gets the value of the specified field from this year-quarter as an |
|
public |
getLong(field: TemporalField): number Gets the value of the specified field from this year-quarter as a |
|
public |
hashCode(): number A hash code for this year-quarter. |
|
public |
isAfter(other: YearQuarter): boolean Is this year-quarter after the specified year-quarter. |
|
public |
isBefore(other: YearQuarter): boolean Is this year-quarter before the specified year-quarter. |
|
public |
isLeapYear(): boolean Checks if the year is a leap year, according to the ISO proleptic calendar system rules. |
|
public |
isSupported(fieldOrUnit: TemporalField | TemporalUnit): boolean function overloading for YearWeek.isSupported
|
|
public |
isValidDay(dayOfQuarter: number): boolean Checks if the day-of-quarter is valid for this year-quarter. |
|
public |
lengthOfQuarter(): number Returns the length of the quarter, taking account of the year. |
|
public |
lengthOfYear(): number Returns the length of the year. |
|
public |
minusQuarters(quartersToSubtract: number): YearQuarter Returns a copy of this year-quarter with the specified period in quarters subtracted. |
|
public |
minusYears(yearsToSubtract: number): YearQuarter Returns a copy of this year-quarter with the specified period in years subtracted. |
|
public |
plusQuarters(quartersToAdd: number): YearQuarter Returns a copy of this year-quarter with the specified period in quarters added. |
|
public |
plusYears(yearsToAdd: number): YearQuarter Returns a copy of this year-quarter with the specified period in years added. |
|
public |
Gets the quarter-of-year field using the |
|
public |
quarterValue(): number Gets the quarter-of-year field from 1 to 4. |
|
public |
query(query: TemporalQuery): * Queries this year-quarter using the specified query. |
|
public |
range(field: TemporalField): ValueRange Gets the range of valid values for the specified field. |
|
public |
toString(): string Outputs this year-quarter as a |
|
public |
until(endExclusive: Temporal, unit: TemporalUnit): number Calculates the amount of time until another year-quarter in terms of the specified unit. |
|
public |
withQuarter(quarter: number): YearQuarter Returns a copy of this |
|
public |
withYear(year: number): YearQuarter Returns a copy of this |
|
public |
year(): number Gets the year field. |
Protected Methods | ||
protected |
_isSupportedField(field: TemporalField): boolean Checks if the specified field is supported. |
|
protected |
_isSupportedUnit(unit: TemporalUnit): boolean Checks if the specified unit is supported. |
Static Public Methods
public static from(temporal: TemporalAccessor): YearQuarter source
Obtains an instance of YearQuarter
from a temporal object.
This obtains a year-quarter based on the specified temporal.
A TemporalAccessor
represents an arbitrary set of date and time information,
which this factory converts to an instance of YearQuarter
.
The conversion extracts the ChronoField.YEAR and
IsoFields.QUARTER_OF_YEAR fields.
The extraction is only permitted if the temporal object has an ISO
chronology, or can be converted to a LocalDate
.
This method matches the signature of the functional interface TemporalQuery
allowing it to be used in queries via method reference, YearQuarter.FROM
.
Params:
Name | Type | Attribute | Description |
temporal | TemporalAccessor | the temporal object to convert, not null |
Throw:
if unable to convert to a |
public static now(zoneIdOrClock: ZoneId | Clock): YearQuarter source
function overloading for YearQuarter.now
- if called with 0 argument YearQuarter._now0 is executed,
- if called with 1 argument and first argument is an instance of ZoneId, then YearQuarter._nowZoneId is executed,
- otherwise YearQuarter._nowClock is executed
public static of(year: Year | number, quarter: Quarter | number): YearQuarter source
function overloading for YearQuarter.of
- if called with {Year} and {Quarter} YearQuarter._ofYearQuarter is executed,
- if called with {Year} and {number} YearQuarter._ofYearInt is executed,
- if called with {number} and {Quarter} YearQuarter._ofIntQuarter is executed,
- if called with {number} and {number} YearQuarter._ofIntInt is executed, otherwise throws IllegalArgumentException.
public static parse(text: string, formatter: DateTimeFormatter): YearQuarter source
Obtains an instance of YearQuarter
from a text string using a specific formatter.
The text is parsed using the formatter, returning a year-quarter.
Params:
Name | Type | Attribute | Description |
text | string | the text to parse, not null |
|
formatter | DateTimeFormatter |
|
the formatter to use, default is YearQuarter.PARSER |
Throw:
if the text cannot be parsed |
Static Protected Methods
protected static _now0(): YearQuarter source
Obtains the current year-quarter from the system clock in the default time-zone.
This will query the Clock.systemDefaultZone system clock in the default time-zone to obtain the current year-quarter. The zone and offset will be set based on the time-zone in the clock.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
protected static _nowZoneId(zone: ZoneId): YearQuarter source
Obtains the current year-quarter from the system clock in the specified time-zone.
This will query the Clock.system to obtain the current year-quarter. Specifying the time-zone avoids dependence on the default time-zone.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
Params:
Name | Type | Attribute | Description |
zone | ZoneId | the zone ID to use, not null |
protected static _ofIntInt(year: number, quarter: number): YearQuarter source
Obtains an instance of YearQuarter
from a year and quarter.
Params:
Name | Type | Attribute | Description |
year | number | the year to represent, from MIN_YEAR to MAX_YEAR |
|
quarter | number | the quarter-of-year to represent, from 1 to 4 |
Throw:
if either field value is invalid |
protected static _ofIntQuarter(year: number, quarter: Quarter): YearQuarter source
Obtains an instance of YearQuarter
from a year and quarter.
Params:
Name | Type | Attribute | Description |
year | number | the year to represent, from MIN_YEAR to MAX_YEAR |
|
quarter | Quarter | the quarter-of-year to represent, not null |
Throw:
if the year value is invalid |
protected static _ofYearInt(year: Year, quarter: number): YearQuarter source
Obtains an instance of YearQuarter
from a year and quarter.
Params:
Name | Type | Attribute | Description |
year | Year | the year to represent, not null |
|
quarter | number | the quarter-of-year to represent, from 1 to 4 |
Throw:
if the quarter value is invalid |
protected static _ofYearQuarter(year: Year, quarter: Quarter): YearQuarter source
Obtains an instance of YearQuarter
from a year and quarter.
Public Methods
public adjustInto(temporal: Temporal): Temporal source
Adjusts the specified temporal object to have this year-quarter.
This returns a temporal object of the same observable type as the input with the year and quarter changed to be the same as this.
The adjustment is equivalent to using Temporal.plus
passing the number of quarters to adjust by.
If the specified temporal object does not use the ISO calendar system then
a DateTimeException
is thrown.
In most cases, it is clearer to reverse the calling pattern by using Temporal.with:
// these two lines are equivalent, but the second approach is recommended
temporal = thisYearQuarter.adjustInto(temporal);
temporal = temporal.with(thisYearQuarter);
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
temporal | Temporal | the target object to be adjusted, not null |
Throw:
if unable to make the adjustment |
|
if numeric overflow occurs |
public atDay(dayOfQuarter: number): LocalDate source
Combines this year-quarter with a day-of-quarter to create a LocalDate
.
This returns a LocalDate
formed from this year-quarter and the specified day-of-quarter.
The day-of-quarter value must be valid for the year-quarter.
This method can be used as part of a chain to produce a date:
LocalDate date = yearQuarter.atDay(day);
Params:
Name | Type | Attribute | Description |
dayOfQuarter | number | the day-of-quarter to use, from 1 to 92 |
Throw:
if the day is invalid for the year-quarter |
public atEndOfQuarter(): LocalDate source
Returns a LocalDate
at the end of the quarter.
This returns a LocalDate
based on this year-quarter.
The day-of-quarter is set to the last valid day of the quarter, taking
into account leap years.
This method can be used as part of a chain to produce a date:
LocalDate date = year.atQuarter(quarter).atEndOfQuarter();
public compareTo(other: YearQuarter): number source
Compares this year-quarter to another
The comparison is based first on the value of the year, then on the value of the quarter. It is 'consistent with equals', as defined by Comparable.
Params:
Name | Type | Attribute | Description |
other | YearQuarter | the other year-quarter to compare to, not null |
Return:
number | the comparator value, negative if less, positive if greater |
public equals(obj: *): boolean source
Checks if this year-quarter is equal to another year-quarter.
The comparison is based on the time-line position of the year-quarters.
Params:
Name | Type | Attribute | Description |
obj | * | the object to check, null returns false |
Return:
boolean | true if this is equal to the other year-quarter |
public format(formatter: DateTimeFormatter): string source
Formats this year-quarter using the specified formatter.
This year-quarter will be passed to the formatter to produce a string.
Params:
Name | Type | Attribute | Description |
formatter | DateTimeFormatter | the formatter to use, not null |
Return:
string | the formatted year-quarter string, not null |
Throw:
if an error occurs during printing |
public get(field: TemporalField): number source
Gets the value of the specified field from this year-quarter as an int
.
This queries this year-quarter for the value for the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a ChronoField then the query is implemented here.
The YearQuarter.isSupported supported fields will return valid
values based on this year-quarter,.
All other ChronoField
instances will throw an UnsupportedTemporalTypeException
.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.getFrom(TemporalAccessor)
passing this
as the argument. Whether the value can be obtained,
and what the value represents, is determined by the field.
Params:
Name | Type | Attribute | Description |
field | TemporalField | the field to get, not null |
Return:
number | the value for the field |
Throw:
if a value for the field cannot be obtained or the value is outside the range of valid values for the field |
|
if the field is not supported or
the range of values exceeds an |
|
if numeric overflow occurs |
public getLong(field: TemporalField): number source
Gets the value of the specified field from this year-quarter as a long
.
This queries this year-quarter for the value for the specified field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a ChronoField then the query is implemented here.
The YearQuarter.isSupported supported fields will return valid
values based on this year-quarter.
All other ChronoField
instances will throw an UnsupportedTemporalTypeException
.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.getFrom(TemporalAccessor)
passing this
as the argument. Whether the value can be obtained,
and what the value represents, is determined by the field.
Params:
Name | Type | Attribute | Description |
field | TemporalField | the field to get, not null |
Return:
number | the value for the field |
Throw:
if a value for the field cannot be obtained |
|
if the field is not supported |
|
if numeric overflow occurs |
public hashCode(): number source
A hash code for this year-quarter.
Return:
number | a suitable hash code |
public isAfter(other: YearQuarter): boolean source
Is this year-quarter after the specified year-quarter.
Params:
Name | Type | Attribute | Description |
other | YearQuarter | the other year-quarter to compare to, not null |
Return:
boolean | true if this is after the specified year-quarter |
public isBefore(other: YearQuarter): boolean source
Is this year-quarter before the specified year-quarter.
Params:
Name | Type | Attribute | Description |
other | YearQuarter | the other year-quarter to compare to, not null |
Return:
boolean | true if this point is before the specified year-quarter |
public isLeapYear(): boolean source
Checks if the year is a leap year, according to the ISO proleptic calendar system rules.
This method applies the current rules for leap years across the whole time-line. In general, a year is a leap year if it is divisible by four without remainder. However, years divisible by 100, are not leap years, with the exception of years divisible by 400 which are.
For example, 1904 is a leap year it is divisible by 4. 1900 was not a leap year as it is divisible by 100, however 2000 was a leap year as it is divisible by 400.
The calculation is proleptic - applying the same rules into the far future and far past. This is historically inaccurate, but is correct for the ISO-8601 standard.
Return:
boolean | true if the year is leap, false otherwise |
public isSupported(fieldOrUnit: TemporalField | TemporalUnit): boolean source
function overloading for YearWeek.isSupported
- if called with an instance of TemporalField, then YearWeek.isSupportedField is executed,
- if called with an instance of TemporalUnit, then YearWeek.isSupportedUnit is executed,
- otherwise IllegalArgumentException is thrown.
Params:
Name | Type | Attribute | Description |
fieldOrUnit | TemporalField | TemporalUnit |
Return:
boolean |
public isValidDay(dayOfQuarter: number): boolean source
Checks if the day-of-quarter is valid for this year-quarter.
This method checks whether this year and quarter and the input day form a valid date.
Params:
Name | Type | Attribute | Description |
dayOfQuarter | number | the day-of-quarter to validate, from 1 to 92, invalid value returns false |
Return:
boolean | true if the day is valid for this year-quarter |
public lengthOfQuarter(): number source
Returns the length of the quarter, taking account of the year.
This returns the length of the quarter in days.
Return:
number | the length of the quarter in days, from 90 to 92 |
public lengthOfYear(): number source
Returns the length of the year.
This returns the length of the year in days, either 365 or 366.
Return:
number | 366 if the year is leap, 365 otherwise |
public minusQuarters(quartersToSubtract: number): YearQuarter source
Returns a copy of this year-quarter with the specified period in quarters subtracted.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
quartersToSubtract | number | the quarters to subtract, may be negative |
Throw:
if the result exceeds the supported range |
public minusYears(yearsToSubtract: number): YearQuarter source
Returns a copy of this year-quarter with the specified period in years subtracted.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
yearsToSubtract | number | the years to subtract, may be negative |
Throw:
if the result exceeds the supported range |
public plusQuarters(quartersToAdd: number): YearQuarter source
Returns a copy of this year-quarter with the specified period in quarters added.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
quartersToAdd | number | the quarters to add, may be negative |
Throw:
if the result exceeds the supported range |
public plusYears(yearsToAdd: number): YearQuarter source
Returns a copy of this year-quarter with the specified period in years added.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
yearsToAdd | number | the years to add, may be negative |
Throw:
if the result exceeds the supported range |
public quarter(): Quarter source
Gets the quarter-of-year field using the Quarter
enum.
This method returns the enum Quarter for the quarter.
This avoids confusion as to what int
values mean.
If you need access to the primitive int
value then the enum
provides the Quarter.value.
public quarterValue(): number source
Gets the quarter-of-year field from 1 to 4.
This method returns the quarter as an int
from 1 to 4.
Application code is frequently clearer if the enum Quarter
is used by calling YearQuarter.getQuarter.
Return:
number | the quarter-of-year, from 1 to 4 |
public query(query: TemporalQuery): * source
Queries this year-quarter using the specified query.
This queries this year-quarter using the specified query strategy object.
The TemporalQuery
object defines the logic to be used to
obtain the result. Read the documentation of the query to understand
what the result of this method will be.
The result of this method is obtained by invoking the
TemporalQuery.queryFrom method on the
specified query passing this
as the argument.
Params:
Name | Type | Attribute | Description |
query | TemporalQuery | the query to invoke, not null |
Return:
* | the query result, null may be returned (defined by the query) |
Throw:
if unable to query (defined by the query) |
|
if numeric overflow occurs (defined by the query) |
public range(field: TemporalField): ValueRange source
Gets the range of valid values for the specified field.
The range object expresses the minimum and maximum valid values for a field. This year-quarter is used to enhance the accuracy of the returned range. If it is not possible to return the range, because the field is not supported or for some other reason, an exception is thrown.
If the field is a ChronoField then the query is implemented here.
The YearQuarter.isSupported supported fields will return
appropriate range instances.
All other ChronoField
instances will throw an UnsupportedTemporalTypeException
.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.rangeRefinedBy(TemporalAccessor)
passing this
as the argument.
Whether the range can be obtained is determined by the field.
Params:
Name | Type | Attribute | Description |
field | TemporalField | the field to query the range for, not null |
Throw:
if the range for the field cannot be obtained |
|
if the field is not supported |
public toString(): string source
Outputs this year-quarter as a String
, such as 2007-Q2
.
The output will be in the format uuuu-'Q'Q
:
Return:
string | a string representation of this year-quarter, not null |
public until(endExclusive: Temporal, unit: TemporalUnit): number source
Calculates the amount of time until another year-quarter in terms of the specified unit.
This calculates the amount of time between two YearQuarter
objects in terms of a single TemporalUnit
.
The start and end points are this
and the specified year-quarter.
The result will be negative if the end is before the start.
The Temporal
passed to this method is converted to a
YearQuarter
using YearQuarter.from.
For example, the period in years between two year-quarters can be calculated
using startYearQuarter.until(endYearQuarter, YEARS)
.
The calculation returns a whole number, representing the number of complete units between the two year-quarters. For example, the period in decades between 2012-Q3 and 2032-Q2 will only be one decade as it is one quarter short of two decades.
There are two equivalent ways of using this method. The first is to invoke this method. The second is to use TemporalUnit.between:
// these two lines are equivalent amount = start.until(end, QUARTER_YEARS); amount = QUARTER_YEARS.between(start, end);The choice should be made based on which makes the code more readable.
The calculation is implemented in this method for ChronoUnit.
The units QUARTER_YEARS
, YEARS
, DECADES
,
CENTURIES
, MILLENNIA
and ERAS
are supported.
Other ChronoUnit
values will throw an exception.
If the unit is not a ChronoUnit
, then the result of this method
is obtained by invoking TemporalUnit.between(Temporal, Temporal)
passing this
as the first argument and the converted input temporal
as the second argument.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
endExclusive | Temporal | the end date, exclusive, which is converted to a |
|
unit | TemporalUnit | the unit to measure the amount in, not null |
Return:
number | the amount of time between this year-quarter and the end year-quarter |
Throw:
if the amount cannot be calculated, or the end
temporal cannot be converted to a |
|
if the unit is not supported |
|
if numeric overflow occurs |
public withQuarter(quarter: number): YearQuarter source
Returns a copy of this YearQuarter
with the quarter-of-year altered.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
quarter | number | the quarter-of-year to set in the returned year-quarter, from 1 to 4 |
Throw:
if the quarter-of-year value is invalid |
public withYear(year: number): YearQuarter source
Returns a copy of this YearQuarter
with the year altered.
This instance is immutable and unaffected by this method call.
Params:
Name | Type | Attribute | Description |
year | number | the year to set in the returned year-quarter, from MIN_YEAR to MAX_YEAR |
Throw:
if the year value is invalid |
public year(): number source
Gets the year field.
This method returns the primitive int
value for the year.
The year returned by this method is proleptic as per get(YEAR)
.
Return:
number | the year, from MIN_YEAR to MAX_YEAR |
Protected Methods
protected _isSupportedField(field: TemporalField): boolean source
Checks if the specified field is supported.
This checks if this year-quarter can be queried for the specified field. If false, then calling the YearQuarter.range, YearQuarter.get and YearQuarter.with methods will throw an exception.
If the field is a ChronoField then the query is implemented here. The supported fields are:
QUARTER_OF_YEAR
YEAR_OF_ERA
YEAR
ERA
All other ChronoField
instances will return false.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.isSupportedBy(TemporalAccessor)
passing this
as the argument.
Whether the field is supported is determined by the field.
Params:
Name | Type | Attribute | Description |
field | TemporalField | the field to check, null returns false |
Return:
boolean | true if the field is supported on this year-quarter, false if not |
protected _isSupportedUnit(unit: TemporalUnit): boolean source
Checks if the specified unit is supported.
This checks if the specified unit can be added to, or subtracted from, this year-quarter. If false, then calling the YearQuarter.plus and YearQuarter.minus methods will throw an exception.
If the unit is a ChronoUnit then the query is implemented here. The supported units are:
QUARTER_YEARS
YEARS
DECADES
CENTURIES
MILLENNIA
ERAS
All other ChronoUnit
instances will return false.
If the unit is not a ChronoUnit
, then the result of this method
is obtained by invoking TemporalUnit.isSupportedBy(Temporal)
passing this
as the argument.
Whether the unit is supported is determined by the unit.
Params:
Name | Type | Attribute | Description |
unit | TemporalUnit | the unit to check, null returns false |
Return:
boolean | true if the unit can be added/subtracted, false if not |