This set of units provide unit-based access to manipulate a date, time or date-time.
The standard set of units can be extended by implementing TemporalUnit.
These units are intended to be applicable in multiple calendar systems. For example, most non-ISO calendar systems define units of years, months and days, just with slightly different rules. The documentation of each unit explains how it operates.
Implementation Specification
This is a final, immutable and thread-safe enum.
| Modifier and Type | Field and Description |
|---|---|
| public static final ChronoUnit | CENTURIES
Unit that represents the concept of a century. |
| public static final ChronoUnit | DAYS
Unit that represents the concept of a day. |
| public static final ChronoUnit | DECADES
Unit that represents the concept of a decade. |
| private final Duration | |
| public static final ChronoUnit | ERAS
Unit that represents the concept of an era. |
| public static final ChronoUnit | FOREVER
Artificial unit that represents the concept of forever. |
| public static final ChronoUnit | HALF_DAYS
Unit that represents the concept of half a day, as used in AM/PM. |
| public static final ChronoUnit | HOURS
Unit that represents the concept of an hour. |
| public static final ChronoUnit | MICROS
Unit that represents the concept of a microsecond. |
| public static final ChronoUnit | MILLENNIA
Unit that represents the concept of a millennium. |
| public static final ChronoUnit | MILLIS
Unit that represents the concept of a millisecond. |
| public static final ChronoUnit | MINUTES
Unit that represents the concept of a minute. |
| public static final ChronoUnit | MONTHS
Unit that represents the concept of a month. |
| private final String | name
Hides java. |
| public static final ChronoUnit | NANOS
Unit that represents the concept of a nanosecond, the smallest supported unit of time. |
| public static final ChronoUnit | SECONDS
Unit that represents the concept of a second. |
| public static final ChronoUnit | WEEKS
Unit that represents the concept of a week. |
| public static final ChronoUnit | YEARS
Unit that represents the concept of a year. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public <R extends Temporal> R | addTo(R temporal, long amount)
Implements java. Returns a copy of the specified temporal object with the specified period added. |
| public long | between(Temporal temporal1Inclusive, Temporal temporal2Exclusive)
Implements java. Calculates the amount of time between two temporal objects. |
| public Duration | Returns: the estimated duration of this unit, not nullImplements java. Gets the estimated duration of this unit in the ISO calendar system. |
| public boolean | Returns: true if a date unit, false if a time unitImplements java. Checks if this unit is a date unit. |
| public boolean | Returns: true if the duration is estimated, false if accurateImplements java. Checks if the duration of the unit is an estimate. |
| public boolean | isSupportedBy(Temporal temporal)
Overrides default java. Checks if this unit is supported by the specified temporal object. |
| public boolean | Returns: true if a time unit, false if a date unitImplements java. Checks if this unit is a time unit. |
| public String | toString()
Overrides java. Implements java. Gets a descriptive name for the unit. |
| public static ChronoUnit | |
| public static ChronoUnit[] |
| CENTURIES | back to summary |
|---|---|
| public static final ChronoUnit CENTURIES Unit that represents the concept of a century. For the ISO calendar system, it is equal to 100 years. When used with other calendar systems it must correspond to an integral number of days and is normally an integral number of years. | |
| DAYS | back to summary |
|---|---|
| public static final ChronoUnit DAYS Unit that represents the concept of a day.
For the ISO calendar system, it is the standard day from midnight to midnight.
The estimated duration of a day is When used with other calendar systems it must correspond to the day defined by the rising and setting of the Sun on Earth. It is not required that days begin at midnight - when converting between calendar systems, the date should be equivalent at midday. | |
| DECADES | back to summary |
|---|---|
| public static final ChronoUnit DECADES Unit that represents the concept of a decade. For the ISO calendar system, it is equal to 10 years. When used with other calendar systems it must correspond to an integral number of days and is normally an integral number of years. | |
| duration | back to summary |
|---|---|
| private final Duration duration | |
| ERAS | back to summary |
|---|---|
| public static final ChronoUnit ERAS Unit that represents the concept of an era.
The ISO calendar system doesn't have eras thus it is impossible to add
an era to a date or date-time.
The estimated duration of the era is artificially defined as When used with other calendar systems there are no restrictions on the unit. | |
| FOREVER | back to summary |
|---|---|
| public static final ChronoUnit FOREVER Artificial unit that represents the concept of forever.
This is primarily used with | |
| HALF_DAYS | back to summary |
|---|---|
| public static final ChronoUnit HALF_DAYS Unit that represents the concept of half a day, as used in AM/PM. For the ISO calendar system, it is equal to 12 hours. | |
| HOURS | back to summary |
|---|---|
| public static final ChronoUnit HOURS Unit that represents the concept of an hour. For the ISO calendar system, it is equal to 60 minutes. | |
| MICROS | back to summary |
|---|---|
| public static final ChronoUnit MICROS Unit that represents the concept of a microsecond. For the ISO calendar system, it is equal to the 1,000,000th part of the second unit. | |
| MILLENNIA | back to summary |
|---|---|
| public static final ChronoUnit MILLENNIA Unit that represents the concept of a millennium. For the ISO calendar system, it is equal to 1000 years. When used with other calendar systems it must correspond to an integral number of days and is normally an integral number of years. | |
| MILLIS | back to summary |
|---|---|
| public static final ChronoUnit MILLIS Unit that represents the concept of a millisecond. For the ISO calendar system, it is equal to the 1000th part of the second unit. | |
| MINUTES | back to summary |
|---|---|
| public static final ChronoUnit MINUTES Unit that represents the concept of a minute. For the ISO calendar system, it is equal to 60 seconds. | |
| MONTHS | back to summary |
|---|---|
| public static final ChronoUnit MONTHS Unit that represents the concept of a month.
For the ISO calendar system, the length of the month varies by month-of-year.
The estimated duration of a month is one twelfth of When used with other calendar systems it must correspond to an integral number of days. | |
| name | back to summary |
|---|---|
| private final String name Hides java. | |
| NANOS | back to summary |
|---|---|
| public static final ChronoUnit NANOS Unit that represents the concept of a nanosecond, the smallest supported unit of time. For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit. | |
| SECONDS | back to summary |
|---|---|
| public static final ChronoUnit SECONDS Unit that represents the concept of a second. For the ISO calendar system, it is equal to the second in the SI system of units, except around a leap-second. | |
| WEEKS | back to summary |
|---|---|
| public static final ChronoUnit WEEKS Unit that represents the concept of a week. For the ISO calendar system, it is equal to 7 days. When used with other calendar systems it must correspond to an integral number of days. | |
| YEARS | back to summary |
|---|---|
| public static final ChronoUnit YEARS Unit that represents the concept of a year.
For the ISO calendar system, it is equal to 12 months.
The estimated duration of a year is When used with other calendar systems it must correspond to an integral number of days or months roughly equal to a year defined by the passage of the Earth around the Sun. | |
| ChronoUnit | back to summary |
|---|---|
| private ChronoUnit(String name, Duration estimatedDuration) | |
| addTo | back to summary |
|---|---|
| public <R extends Temporal> R addTo(R temporal, long amount) Implements java. Doc from java. Returns a copy of the specified temporal object with the specified period added. The period added is a multiple of this unit. For example, this method could be used to add "3 days" to a date by calling this method on the instance representing "days", passing the date and the period "3". The period to be added may be negative, which is equivalent to subtraction.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use // these two lines are equivalent, but the second approach is recommended temporal = thisUnit.addTo(temporal, amount); temporal = temporal.plus(amount, thisUnit);It is recommended to use the second approach, plus(amount, thisUnit),
as it is a lot clearer to read in code.
Implementations should perform any queries or calculations using the units
available in Implementations must not alter the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
| |
| between | back to summary |
|---|---|
| public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) Implements java. Doc from java. Calculates the amount of time between two temporal objects.
This calculates the amount in terms of this unit. The start and end
points are supplied as temporal objects and must be of compatible types.
The implementation will convert the second type to be an instance of the
first type before the calculating the amount.
The result will be negative if the end is before the start.
For example, the amount in hours between two temporal objects can be
calculated using The calculation returns a whole number, representing the number of complete units between the two temporals. If there are smaller unit fields, their values are considered when determining the final whole number. For example, the amount in hours between the times 11:30 and 13:29 will only be one hour as it is one minute short of two hours, or the amount in months between the dates 2024-09-29 and 2025-02-28 (the last day in February) will be 4 months as it is one day short of 5 months.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use // these two lines are equivalent between = thisUnit.between(start, end); between = start.until(end, thisUnit);The choice should be made based on which makes the code more readable. For example, this method allows the number of days between two dates to be calculated: long daysBetween = DAYS.between(start, end); // or alternatively long daysBetween = start.until(end, DAYS);
Implementations should perform any queries or calculations using the units
available in
| |
| getDuration | back to summary |
|---|---|
| public Duration getDuration() Implements java. Gets the estimated duration of this unit in the ISO calendar system. All of the units in this class have an estimated duration. Days vary due to daylight saving time, while months have different lengths. | |
| isDateBased | back to summary |
|---|---|
| public boolean isDateBased() Implements java. Checks if this unit is a date unit.
All units from days to eras inclusive are date-based.
Time-based units and
| |
| isDurationEstimated | back to summary |
|---|---|
| public boolean isDurationEstimated() Implements java. Checks if the duration of the unit is an estimate. All time units in this class are considered to be accurate, while all date units in this class are considered to be estimated. This definition ignores leap seconds, but considers that Days vary due to daylight saving time and months have different lengths.
| |
| isSupportedBy | back to summary |
|---|---|
| public boolean isSupportedBy(Temporal temporal) Overrides default java. Doc from java. Checks if this unit is supported by the specified temporal object. This checks that the implementing date-time can add/subtract this unit. This can be used to avoid throwing an exception.
This default implementation derives the value using
| |
| isTimeBased | back to summary |
|---|---|
| public boolean isTimeBased() Implements java. Checks if this unit is a time unit.
All units from nanos to half-days inclusive are time-based.
Date-based units and
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Implements java. Doc from java. Gets a descriptive name for the unit. This should be in the plural and upper-first camel case, such as 'Days' or 'Minutes'.
| |
| valueOf | back to summary |
|---|---|
| public static ChronoUnit valueOf(String name) | |
| values | back to summary |
|---|---|
| public static ChronoUnit[] values() | |