Serializable
, Comparable<ChronoUnit>
, TemporalUnit
public enum ChronoUnit extends Enum<ChronoUnit> implements TemporalUnit
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.
Enum Constant | Description |
---|---|
CENTURIES |
Unit that represents the concept of a century.
|
DAYS |
Unit that represents the concept of a day.
|
DECADES |
Unit that represents the concept of a decade.
|
ERAS |
Unit that represents the concept of an era.
|
FOREVER |
Artificial unit that represents the concept of forever.
|
HALF_DAYS |
Unit that represents the concept of half a day, as used in AM/PM.
|
HOURS |
Unit that represents the concept of an hour.
|
MICROS |
Unit that represents the concept of a microsecond.
|
MILLENNIA |
Unit that represents the concept of a millennium.
|
MILLIS |
Unit that represents the concept of a millisecond.
|
MINUTES |
Unit that represents the concept of a minute.
|
MONTHS |
Unit that represents the concept of a month.
|
NANOS |
Unit that represents the concept of a nanosecond, the smallest supported unit of time.
|
SECONDS |
Unit that represents the concept of a second.
|
WEEKS |
Unit that represents the concept of a week.
|
YEARS |
Unit that represents the concept of a year.
|
Modifier and Type | Method | Description |
---|---|---|
<R extends Temporal> |
addTo(R temporal,
long amount) |
Returns a copy of the specified temporal object with the specified period added.
|
long |
between(Temporal temporal1Inclusive,
Temporal temporal2Exclusive) |
Calculates the amount of time between two temporal objects.
|
Duration |
getDuration() |
Gets the estimated duration of this unit in the ISO calendar system.
|
boolean |
isDateBased() |
Checks if this unit is a date unit.
|
boolean |
isDurationEstimated() |
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 |
isTimeBased() |
Checks if this unit is a time unit.
|
String |
toString() |
Returns the name of this enum constant, as contained in the
declaration.
|
static ChronoUnit |
valueOf(String name) |
Returns the enum constant of this type with the specified name.
|
static ChronoUnit[] |
values() |
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final ChronoUnit NANOS
public static final ChronoUnit MICROS
public static final ChronoUnit MILLIS
public static final ChronoUnit SECONDS
public static final ChronoUnit MINUTES
public static final ChronoUnit HOURS
public static final ChronoUnit HALF_DAYS
public static final ChronoUnit DAYS
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.
public static final ChronoUnit WEEKS
When used with other calendar systems it must correspond to an integral number of days.
public static final ChronoUnit MONTHS
365.2425 Days
.
When used with other calendar systems it must correspond to an integral number of days.
public static final ChronoUnit YEARS
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 static final ChronoUnit DECADES
When used with other calendar systems it must correspond to an integral number of days and is normally an integral number of years.
public static final ChronoUnit CENTURIES
When used with other calendar systems it must correspond to an integral number of days and is normally an integral number of years.
public static final ChronoUnit MILLENNIA
When used with other calendar systems it must correspond to an integral number of days and is normally an integral number of years.
public static final ChronoUnit ERAS
1,000,000,000 Years
.
When used with other calendar systems there are no restrictions on the unit.
public static final ChronoUnit FOREVER
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
.public static ChronoUnit[] values()
for (ChronoUnit c : ChronoUnit.values()) System.out.println(c);
public static ChronoUnit valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic Duration getDuration()
All of the units in this class have an estimated duration. Days vary due to daylight saving time, while months have different lengths.
getDuration
in interface TemporalUnit
public boolean isDurationEstimated()
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.
isDurationEstimated
in interface TemporalUnit
public boolean isDateBased()
All units from days to eras inclusive are date-based.
Time-based units and FOREVER
return false.
isDateBased
in interface TemporalUnit
public boolean isTimeBased()
All units from nanos to half-days inclusive are time-based.
Date-based units and FOREVER
return false.
isTimeBased
in interface TemporalUnit
public boolean isSupportedBy(Temporal temporal)
TemporalUnit
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)
.
isSupportedBy
in interface TemporalUnit
temporal
- the temporal object to check, not nullpublic <R extends Temporal> R addTo(R temporal, long amount)
TemporalUnit
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);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.
addTo
in interface TemporalUnit
R
- the type of the Temporal objecttemporal
- the temporal object to adjust, not nullamount
- the amount of this unit to add, positive or negativepublic long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive)
TemporalUnit
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);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.
between
in interface TemporalUnit
temporal1Inclusive
- the base temporal object, not nulltemporal2Exclusive
- the other temporal object, exclusive, not nullpublic String toString()
Enum
toString
in interface TemporalUnit
toString
in class Enum<ChronoUnit>
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-internal+0-adhoc.mlchung.jdk9-jdeps