ChronoUnit
class ChronoUnit : TemporalUnit
| kotlin.Any | ||
| ↳ | kotlin.Enum<java.time.temporal.ChronoUnit> | |
| ↳ | java.time.temporal.ChronoUnit | |
A standard set of date periods units.
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.
Summary
| Enum values | |
|---|---|
|
Unit that represents the concept of a century. |
|
|
Unit that represents the concept of a day. |
|
|
Unit that represents the concept of a decade. |
|
|
Unit that represents the concept of an era. |
|
|
Artificial unit that represents the concept of forever. |
|
|
Unit that represents the concept of half a day, as used in AM/PM. |
|
|
Unit that represents the concept of an hour. |
|
|
Unit that represents the concept of a microsecond. |
|
|
Unit that represents the concept of a millennium. |
|
|
Unit that represents the concept of a millisecond. |
|
|
Unit that represents the concept of a minute. |
|
|
Unit that represents the concept of a month. |
|
|
Unit that represents the concept of a nanosecond, the smallest supported unit of time. |
|
|
Unit that represents the concept of a second. |
|
|
Unit that represents the concept of a week. |
|
|
Unit that represents the concept of a year. |
|
| Public methods | |
|---|---|
| R |
Returns a copy of the specified temporal object with the specified period added. |
| Long |
Calculates the amount of time between two temporal objects. |
| Duration! |
Gets the estimated duration of this unit in the ISO calendar system. |
| Boolean |
Checks if this unit is a date unit. |
| Boolean |
Checks if the duration of the unit is an estimate. |
| Boolean |
isSupportedBy(temporal: Temporal!)Checks if this unit is supported by the specified temporal object. |
| Boolean |
Checks if this unit is a time unit. |
| String |
toString()Returns the name of this enum constant, as contained in the declaration. |
Enum values
CENTURIES
enum val CENTURIES : ChronoUnit
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
enum val DAYS : ChronoUnit
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 24 Hours.
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
enum val DECADES : ChronoUnit
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.
ERAS
enum val ERAS : ChronoUnit
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 1,000,000,000 Years.
When used with other calendar systems there are no restrictions on the unit.
FOREVER
enum val FOREVER : ChronoUnit
Artificial unit that represents the concept of forever. This is primarily used with TemporalField to represent unbounded fields such as the year or era. The estimated duration of this unit is artificially defined as the largest duration supported by Duration.
HALF_DAYS
enum val HALF_DAYS : ChronoUnit
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
enum val HOURS : ChronoUnit
Unit that represents the concept of an hour. For the ISO calendar system, it is equal to 60 minutes.
MICROS
enum val MICROS : ChronoUnit
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
enum val MILLENNIA : ChronoUnit
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
enum val MILLIS : ChronoUnit
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
enum val MINUTES : ChronoUnit
Unit that represents the concept of a minute. For the ISO calendar system, it is equal to 60 seconds.
MONTHS
enum val MONTHS : ChronoUnit
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 365.2425 Days.
When used with other calendar systems it must correspond to an integral number of days.
NANOS
enum val NANOS : ChronoUnit
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
enum val SECONDS : ChronoUnit
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
enum val WEEKS : ChronoUnit
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
enum val YEARS : ChronoUnit
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 365.2425 Days.
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.
Public methods
addTo
fun <R : Temporal!> addTo(
temporal: R,
amount: Long
): R
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 Temporal.plus(long, TemporalUnit):
// these two lines are equivalent, but the second approach is recommended temporal = thisUnit.addTo(temporal); temporal = temporal.plus(thisUnit);
plus(TemporalUnit), as it is a lot clearer to read in code.
Implementations should perform any queries or calculations using the units available in ChronoUnit or the fields available in ChronoField. If the unit is not supported an UnsupportedTemporalTypeException must be thrown.
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.
| Parameters | |
|---|---|
<R> |
the type of the Temporal object |
temporal |
R: the temporal object to adjust, not null |
amount |
Long: the amount of this unit to add, positive or negative |
| Return | |
|---|---|
R |
the adjusted temporal object, not null |
| Exceptions | |
|---|---|
java.time.DateTimeException |
if the amount cannot be added |
java.time.temporal.UnsupportedTemporalTypeException |
if the unit is not supported by the temporal |
between
fun between(
temporal1Inclusive: Temporal!,
temporal2Exclusive: Temporal!
): Long
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 HOURS.between(startTime, endTime).
The calculation returns a whole number, representing the number of complete units between the two temporals. 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.
There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use Temporal.until(Temporal, TemporalUnit):
// these two lines are equivalent between = thisUnit.between(start, end); between = start.until(end, thisUnit);
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 ChronoUnit or the fields available in ChronoField. If the unit is not supported an UnsupportedTemporalTypeException must be thrown. Implementations must not alter the specified temporal objects.
| Parameters | |
|---|---|
temporal1Inclusive |
Temporal!: the base temporal object, not null |
temporal2Exclusive |
Temporal!: the other temporal object, exclusive, not null |
| Return | |
|---|---|
Long |
the amount of time between temporal1Inclusive and temporal2Exclusive in terms of this unit; positive if temporal2Exclusive is later than temporal1Inclusive, negative if earlier |
| Exceptions | |
|---|---|
java.lang.ArithmeticException |
if numeric overflow occurs |
java.time.DateTimeException |
if the amount cannot be calculated, or the end temporal cannot be converted to the same type as the start temporal |
java.time.temporal.UnsupportedTemporalTypeException |
if the unit is not supported by the temporal |
getDuration
fun getDuration(): Duration!
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.
| Return | |
|---|---|
Duration! |
the estimated duration of this unit, not null |
isDateBased
fun isDateBased(): Boolean
Checks if this unit is a date unit.
All units from days to eras inclusive are date-based. Time-based units and FOREVER return false.
| Return | |
|---|---|
Boolean |
true if a date unit, false if a time unit |
isDurationEstimated
fun isDurationEstimated(): Boolean
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.
| Return | |
|---|---|
Boolean |
true if the duration is estimated, false if accurate |
isSupportedBy
fun isSupportedBy(temporal: Temporal!): Boolean
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 Temporal.plus(long, TemporalUnit).
| Parameters | |
|---|---|
temporal |
Temporal!: the temporal object to check, not null |
| Return | |
|---|---|
Boolean |
true if the unit is supported |
isTimeBased
fun isTimeBased(): Boolean
Checks if this unit is a time unit.
All units from nanos to half-days inclusive are time-based. Date-based units and FOREVER return false.
| Return | |
|---|---|
Boolean |
true if a time unit, false if a date unit |
toString
fun toString(): String
Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.
| Return | |
|---|---|
String |
the name of this unit, not null |