TemporalAmount
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
ChronoPeriod |
A date-based amount of time, such as '3 years, 4 months and 5 days' in an
arbitrary chronology, intended for advanced globalization use cases. |
Duration |
A time-based amount of time, such as '34.5 seconds'. |
Period |
A date-based amount of time in the ISO-8601 calendar system,
such as '2 years, 3 months and 4 days'. |
|
Framework-level interface defining an amount of time, such as
"6 hours", "8 days" or "2 years and 3 months".
This is the base interface type for amounts of time.
An amount is distinct from a date or time-of-day in that it is not tied
to any specific point on the time-line.
The amount can be thought of as a Map
of TemporalUnit
to
long
, exposed via getUnits()
and get(TemporalUnit)
.
A simple case might have a single unit-value pair, such as "6 hours".
A more complex case may have multiple unit-value pairs, such as
"7 years, 3 months and 5 days".
There are two common implementations.
Period
is a date-based implementation, storing years, months and days.
Duration
is a time-based implementation, storing seconds and nanoseconds,
but providing some access using other duration based units such as minutes,
hours and fixed 24-hour days.
This interface is a framework-level interface that should not be widely
used in application code. Instead, applications should create and pass
around instances of concrete types, such as Period
and Duration
.
Public Methods
public
abstract
Temporal
addTo
(Temporal temporal)
Adds to the specified temporal object.
Adds the amount to the specified temporal object using the logic
encapsulated in the implementing class.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use Temporal.plus(TemporalAmount)
:
// These two lines are equivalent, but the second approach is recommended
dateTime = amount.addTo(dateTime);
dateTime = dateTime.plus(adder);
It is recommended to use the second approach,
plus(TemporalAmount)
,
as it is a lot clearer to read in code.
Parameters
temporal |
the temporal object to add the amount to, not null |
Returns
- an object of the same observable type with the addition made, not null
public
abstract
long
get
(TemporalUnit unit)
Returns the value of the requested unit.
The units returned from getUnits()
uniquely define the
value of the TemporalAmount
. A value must be returned
for each unit listed in getUnits
.
Parameters
unit |
the TemporalUnit for which to return the value |
Returns
- the long value of the unit
Returns the list of units uniquely defining the value of this TemporalAmount.
The list of TemporalUnits
is defined by the implementation class.
The list is a snapshot of the units at the time getUnits
is called and is not mutable.
The units are ordered from longest duration to the shortest duration
of the unit.
Returns
- the List of
TemporalUnits
; not null
public
abstract
Temporal
subtractFrom
(Temporal temporal)
Subtracts this object from the specified temporal object.
Subtracts the amount from the specified temporal object using the logic
encapsulated in the implementing class.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use Temporal.minus(TemporalAmount)
:
// these two lines are equivalent, but the second approach is recommended
dateTime = amount.subtractFrom(dateTime);
dateTime = dateTime.minus(amount);
It is recommended to use the second approach,
minus(TemporalAmount)
,
as it is a lot clearer to read in code.
Parameters
temporal |
the temporal object to subtract the amount from, not null |
Returns
- an object of the same observable type with the subtraction made, not null
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eTemporalAmount\u003c/code\u003e is a framework-level interface representing an amount of time, like "6 hours" or "2 years, 3 months".\u003c/p\u003e\n"],["\u003cp\u003eIt's not tied to a specific point on the timeline and can be considered a map of time units to their quantities.\u003c/p\u003e\n"],["\u003cp\u003eCommon implementations include \u003ccode\u003ePeriod\u003c/code\u003e (date-based, like years, months, days) and \u003ccode\u003eDuration\u003c/code\u003e (time-based, like seconds, nanoseconds).\u003c/p\u003e\n"],["\u003cp\u003eApplications should generally use concrete types like \u003ccode\u003ePeriod\u003c/code\u003e and \u003ccode\u003eDuration\u003c/code\u003e instead of \u003ccode\u003eTemporalAmount\u003c/code\u003e directly.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods to add to and subtract from temporal objects, as well as to query the units and their values.\u003c/p\u003e\n"]]],[],null,["# TemporalAmount\n\npublic interface **TemporalAmount** \n\n|---|---|---|\n| Known Indirect Subclasses [ChronoPeriod](../../../../reference/java/time/chrono/ChronoPeriod.html), [Duration](../../../../reference/java/time/Duration.html), [Period](../../../../reference/java/time/Period.html) |--------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------| | [ChronoPeriod](../../../../reference/java/time/chrono/ChronoPeriod.html) | A date-based amount of time, such as '3 years, 4 months and 5 days' in an arbitrary chronology, intended for advanced globalization use cases. | | [Duration](../../../../reference/java/time/Duration.html) | A time-based amount of time, such as '34.5 seconds'. | | [Period](../../../../reference/java/time/Period.html) | A date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'. | |||\n\nFramework-level interface defining an amount of time, such as\n\"6 hours\", \"8 days\" or \"2 years and 3 months\".\n\n\nThis is the base interface type for amounts of time.\nAn amount is distinct from a date or time-of-day in that it is not tied\nto any specific point on the time-line.\n\n\nThe amount can be thought of as a `Map` of [TemporalUnit](../../../../reference/java/time/temporal/TemporalUnit.html) to\n`long`, exposed via [getUnits()](../../../../reference/java/time/temporal/TemporalAmount.html#getUnits()) and [get(TemporalUnit)](../../../../reference/java/time/temporal/TemporalAmount.html#get(java.time.temporal.TemporalUnit)).\nA simple case might have a single unit-value pair, such as \"6 hours\".\nA more complex case may have multiple unit-value pairs, such as\n\"7 years, 3 months and 5 days\".\n\n\nThere are two common implementations.\n[Period](../../../../reference/java/time/Period.html) is a date-based implementation, storing years, months and days.\n[Duration](../../../../reference/java/time/Duration.html) is a time-based implementation, storing seconds and nanoseconds,\nbut providing some access using other duration based units such as minutes,\nhours and fixed 24-hour days.\n\n\nThis interface is a framework-level interface that should not be widely\nused in application code. Instead, applications should create and pass\naround instances of concrete types, such as `Period` and `Duration`. \n\n### Public Method Summary\n\n|------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Temporal](../../../../reference/java/time/temporal/Temporal.html) | [addTo](../../../../reference/java/time/temporal/TemporalAmount.html#addTo(java.time.temporal.Temporal))([Temporal](../../../../reference/java/time/temporal/Temporal.html) temporal) Adds to the specified temporal object. |\n| abstract long | [get](../../../../reference/java/time/temporal/TemporalAmount.html#get(java.time.temporal.TemporalUnit))([TemporalUnit](../../../../reference/java/time/temporal/TemporalUnit.html) unit) Returns the value of the requested unit. |\n| abstract [List](../../../../reference/java/util/List.html)\\\u003c[TemporalUnit](../../../../reference/java/time/temporal/TemporalUnit.html)\\\u003e | [getUnits](../../../../reference/java/time/temporal/TemporalAmount.html#getUnits())() Returns the list of units uniquely defining the value of this TemporalAmount. |\n| abstract [Temporal](../../../../reference/java/time/temporal/Temporal.html) | [subtractFrom](../../../../reference/java/time/temporal/TemporalAmount.html#subtractFrom(java.time.temporal.Temporal))([Temporal](../../../../reference/java/time/temporal/Temporal.html) temporal) Subtracts this object from the specified temporal object. |\n\nPublic Methods\n--------------\n\n#### public abstract [Temporal](../../../../reference/java/time/temporal/Temporal.html)\n**addTo**\n([Temporal](../../../../reference/java/time/temporal/Temporal.html) temporal)\n\nAdds to the specified temporal object.\n\n\nAdds the amount to the specified temporal object using the logic\nencapsulated in the implementing class.\n\n\nThere are two equivalent ways of using this method.\nThe first is to invoke this method directly.\nThe second is to use [Temporal.plus(TemporalAmount)](../../../../reference/java/time/temporal/Temporal.html#plus(java.time.temporal.TemporalAmount)): \n\n```\n // These two lines are equivalent, but the second approach is recommended\n dateTime = amount.addTo(dateTime);\n dateTime = dateTime.plus(adder);\n \n```\nIt is recommended to use the second approach, `plus(TemporalAmount)`, as it is a lot clearer to read in code.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| temporal | the temporal object to add the amount to, not null |\n|----------|----------------------------------------------------|\n\n##### Returns\n\n- an object of the same observable type with the addition made, not null \n\n##### Throws\n\n| [DateTimeException](../../../../reference/java/time/DateTimeException.html) | if unable to add |\n| [ArithmeticException](../../../../reference/java/lang/ArithmeticException.html) | if numeric overflow occurs |\n|---------------------------------------------------------------------------------|----------------------------|\n\n#### public abstract long\n**get**\n([TemporalUnit](../../../../reference/java/time/temporal/TemporalUnit.html) unit)\n\nReturns the value of the requested unit.\nThe units returned from [getUnits()](../../../../reference/java/time/temporal/TemporalAmount.html#getUnits()) uniquely define the\nvalue of the `TemporalAmount`. A value must be returned\nfor each unit listed in `getUnits`. \n\n##### Parameters\n\n| unit | the `TemporalUnit` for which to return the value |\n|------|--------------------------------------------------|\n\n##### Returns\n\n- the long value of the unit \n\n##### Throws\n\n| [DateTimeException](../../../../reference/java/time/DateTimeException.html) | if a value for the unit cannot be obtained |\n| [UnsupportedTemporalTypeException](../../../../reference/java/time/temporal/UnsupportedTemporalTypeException.html) | if the `unit` is not supported |\n|--------------------------------------------------------------------------------------------------------------------|--------------------------------------------|\n\n#### public abstract [List](../../../../reference/java/util/List.html)\\\u003c[TemporalUnit](../../../../reference/java/time/temporal/TemporalUnit.html)\\\u003e\n**getUnits**\n()\n\nReturns the list of units uniquely defining the value of this TemporalAmount.\nThe list of `TemporalUnits` is defined by the implementation class.\nThe list is a snapshot of the units at the time `getUnits`\nis called and is not mutable.\nThe units are ordered from longest duration to the shortest duration\nof the unit. \n\n##### Returns\n\n- the List of `TemporalUnits`; not null \n\n#### public abstract [Temporal](../../../../reference/java/time/temporal/Temporal.html)\n**subtractFrom**\n([Temporal](../../../../reference/java/time/temporal/Temporal.html) temporal)\n\nSubtracts this object from the specified temporal object.\n\n\nSubtracts the amount from the specified temporal object using the logic\nencapsulated in the implementing class.\n\n\nThere are two equivalent ways of using this method.\nThe first is to invoke this method directly.\nThe second is to use [Temporal.minus(TemporalAmount)](../../../../reference/java/time/temporal/Temporal.html#minus(java.time.temporal.TemporalAmount)): \n\n```\n // these two lines are equivalent, but the second approach is recommended\n dateTime = amount.subtractFrom(dateTime);\n dateTime = dateTime.minus(amount);\n \n```\nIt is recommended to use the second approach, `minus(TemporalAmount)`, as it is a lot clearer to read in code.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| temporal | the temporal object to subtract the amount from, not null |\n|----------|-----------------------------------------------------------|\n\n##### Returns\n\n- an object of the same observable type with the subtraction made, not null \n\n##### Throws\n\n| [DateTimeException](../../../../reference/java/time/DateTimeException.html) | if unable to subtract |\n| [ArithmeticException](../../../../reference/java/lang/ArithmeticException.html) | if numeric overflow occurs |\n|---------------------------------------------------------------------------------|----------------------------|"]]