- Description
- Nested Class Summary
- Enum Constant Summary
- Method Summary
- Enum Constant Details
- Method Details
Enum Class ChronoUnit
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.
-
Nested Class Summary
Nested classes/interfaces declared in class Enum
Enum.EnumDesc<E> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionUnit 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. -
Method Summary
All MethodsStatic MethodsInstance MethodsConcrete MethodsModifier and TypeMethodDescription<R extends Temporal>
RaddTo(R temporal, long amount)Returns a copy of the specified temporal object with the specified period added.longCalculates the amount of time between two temporal objects.Gets the estimated duration of this unit in the ISO calendar system.booleanChecks if this unit is a date unit.booleanChecks if the duration of the unit is an estimate.booleanChecks if this unit is a time unit.toString()Returns the name of this enum constant, as contained in the declaration.static ChronoUnitReturns the enum constant of this class with the specified name.static ChronoUnit[]values()Returns an array containing the constants of this enum class, in the order they are declared.Methods declared in class Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOfModifier and TypeMethodDescriptionprotected final Objectclone()Throws CloneNotSupportedException.final intCompares this enum with the specified object for order.Returns an enum descriptor EnumDesc for this instance, if one can be constructed, or an empty Optional if one cannot be.final booleanReturns true if the specified object is equal to this enum constant.protected final voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization has been deprecated for removal.final Class<ChronoUnit>Returns the Class object corresponding to this enum constant's enum type.final inthashCode()Returns a hash code for this enum constant.final Stringname()Returns the name of this enum constant, exactly as declared in its enum declaration.final intordinal()Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).toString()Returns the name of this enum constant, as contained in the declaration.static <T extends Enum<T>>
TReturns the enum constant of the specified enum class with the specified name.Methods declared in class Object
getClass, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionfinal Class<?>getClass()Returns the runtime class of this Object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis)Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos)Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.Methods declared in interface TemporalUnit
isSupportedByModifier and TypeMethodDescriptiondefault booleanisSupportedBy(Temporal temporal)Checks if this unit is supported by the specified temporal object.
-
Enum Constant Details
-
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. -
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. -
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. -
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. -
MINUTES
Unit that represents the concept of a minute. For the ISO calendar system, it is equal to 60 seconds. -
HOURS
Unit that represents the concept of an hour. For the ISO calendar system, it is equal to 60 minutes. -
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. -
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 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.
-
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.
-
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 365.2425 Days.When used with other calendar systems it must correspond to an integral number of days.
-
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 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.
-
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.
-
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.
-
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.
-
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 1,000,000,000 Years.When used with other calendar systems there are no restrictions on the unit.
-
FOREVER
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 Duration.MAX.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.Returns: an array containing the constants of this enum class, in the order they are declared -
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)Parameters: name - the name of the enum constant to be returned. Returns: the enum constant with the specified name Throws: IllegalArgumentException - if this enum class has no constant with the specified name NullPointerException - if the argument is null -
getDuration
Gets the estimated duration of this unit in the ISO calendar system.Specified by: getDuration in interface TemporalUnit Returns: the estimated duration of this unit, not nullAll of the units in this class have an estimated duration. Days vary due to daylight saving time, while months have different lengths.
-
isDurationEstimated
public boolean isDurationEstimated()Checks if the duration of the unit is an estimate.Specified by: isDurationEstimated in interface TemporalUnit Returns: true if the duration is estimated, false if accurateAll 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.
-
isDateBased
public boolean isDateBased()Checks if this unit is a date unit.Specified by: isDateBased in interface TemporalUnit Returns: true if a date unit, false if a time unitAll units from days to eras inclusive are date-based. Time-based units and FOREVER return false.
-
isTimeBased
public boolean isTimeBased()Checks if this unit is a time unit.Specified by: isTimeBased in interface TemporalUnit Returns: true if a time unit, false if a date unitAll units from nanos to half-days inclusive are time-based. Date-based units and FOREVER return false.
-
addTo
Description copied from interface: TemporalUnitReturns a copy of the specified temporal object with the specified period added.Specified by: addTo in interface TemporalUnit Type Parameters: R - the type of the Temporal object Parameters: temporal - the temporal object to adjust, not null amount - the amount of this unit to add, positive or negative Returns: the adjusted temporal object, not nullThe 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); It is recommended to use the second approach, 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.
-
between
Description copied from interface: TemporalUnitCalculates the amount of time between two temporal objects.Specified by: between in interface TemporalUnit Parameters: temporal1Inclusive - the base temporal object, not null temporal2Exclusive - the other temporal object, exclusive, not null Returns: the amount of time between temporal1Inclusive and temporal2Exclusive in terms of this unit; positive if temporal2Exclusive is later than temporal1Inclusive, negative if earlierThis 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. 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 Temporal.until(Temporal, TemporalUnit): // 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 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.
-
toString
Description copied from class: EnumReturns 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.Specified by: toString in interface TemporalUnit Overrides: toString in class Enum<ChronoUnit> Returns: the name of this enum constant
-
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2026, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.