TemporalAmount
TemporalAmount is an interface in the java.time.temporal package that models an amount of time or date that can be added to or subtracted from temporal objects. It represents a quantity expressed in one or more units, such as years, months, days, seconds, or nanos, and is implemented by the standard classes Duration and Period. Duration covers time-based amounts (seconds and nanoseconds), while Period covers date-based amounts (years, months, and days). TemporalAmount is immutable and describes a fixed amount that can be applied to temporal instances.
Key aspects of TemporalAmount include its ability to express complex quantities and to be used with various
- getUnits(), which returns the list of TemporalUnit values that can express this amount.
- get(TemporalUnit), which returns the amount expressed in a particular unit.
- addTo(Temporal temporal) and subtractFrom(Temporal temporal), which apply the amount to a Temporal object and return a
Usage examples are common in date-time arithmetic. For instance, a LocalDate can be advanced by a Period
TemporalAmount serves as a core abstraction for arithmetic on temporal objects, decoupling the representation of an