Path: blob/master/src/java.base/share/classes/java/time/temporal/ChronoUnit.java
41159 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package java.time.temporal;5758import java.time.Duration;5960/**61* A standard set of date periods units.62* <p>63* This set of units provide unit-based access to manipulate a date, time or date-time.64* The standard set of units can be extended by implementing {@link TemporalUnit}.65* <p>66* These units are intended to be applicable in multiple calendar systems.67* For example, most non-ISO calendar systems define units of years, months and days,68* just with slightly different rules.69* The documentation of each unit explains how it operates.70*71* @implSpec72* This is a final, immutable and thread-safe enum.73*74* @since 1.875*/76public enum ChronoUnit implements TemporalUnit {7778/**79* Unit that represents the concept of a nanosecond, the smallest supported unit of time.80* For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.81*/82NANOS("Nanos", Duration.ofNanos(1)),83/**84* Unit that represents the concept of a microsecond.85* For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.86*/87MICROS("Micros", Duration.ofNanos(1000)),88/**89* Unit that represents the concept of a millisecond.90* For the ISO calendar system, it is equal to the 1000th part of the second unit.91*/92MILLIS("Millis", Duration.ofNanos(1000_000)),93/**94* Unit that represents the concept of a second.95* For the ISO calendar system, it is equal to the second in the SI system96* of units, except around a leap-second.97*/98SECONDS("Seconds", Duration.ofSeconds(1)),99/**100* Unit that represents the concept of a minute.101* For the ISO calendar system, it is equal to 60 seconds.102*/103MINUTES("Minutes", Duration.ofSeconds(60)),104/**105* Unit that represents the concept of an hour.106* For the ISO calendar system, it is equal to 60 minutes.107*/108HOURS("Hours", Duration.ofSeconds(3600)),109/**110* Unit that represents the concept of half a day, as used in AM/PM.111* For the ISO calendar system, it is equal to 12 hours.112*/113HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),114/**115* Unit that represents the concept of a day.116* For the ISO calendar system, it is the standard day from midnight to midnight.117* The estimated duration of a day is {@code 24 Hours}.118* <p>119* When used with other calendar systems it must correspond to the day defined by120* the rising and setting of the Sun on Earth. It is not required that days begin121* at midnight - when converting between calendar systems, the date should be122* equivalent at midday.123*/124DAYS("Days", Duration.ofSeconds(86400)),125/**126* Unit that represents the concept of a week.127* For the ISO calendar system, it is equal to 7 days.128* <p>129* When used with other calendar systems it must correspond to an integral number of days.130*/131WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),132/**133* Unit that represents the concept of a month.134* For the ISO calendar system, the length of the month varies by month-of-year.135* The estimated duration of a month is one twelfth of {@code 365.2425 Days}.136* <p>137* When used with other calendar systems it must correspond to an integral number of days.138*/139MONTHS("Months", Duration.ofSeconds(31556952L / 12)),140/**141* Unit that represents the concept of a year.142* For the ISO calendar system, it is equal to 12 months.143* The estimated duration of a year is {@code 365.2425 Days}.144* <p>145* When used with other calendar systems it must correspond to an integral number of days146* or months roughly equal to a year defined by the passage of the Earth around the Sun.147*/148YEARS("Years", Duration.ofSeconds(31556952L)),149/**150* Unit that represents the concept of a decade.151* For the ISO calendar system, it is equal to 10 years.152* <p>153* When used with other calendar systems it must correspond to an integral number of days154* and is normally an integral number of years.155*/156DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),157/**158* Unit that represents the concept of a century.159* For the ISO calendar system, it is equal to 100 years.160* <p>161* When used with other calendar systems it must correspond to an integral number of days162* and is normally an integral number of years.163*/164CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),165/**166* Unit that represents the concept of a millennium.167* For the ISO calendar system, it is equal to 1000 years.168* <p>169* When used with other calendar systems it must correspond to an integral number of days170* and is normally an integral number of years.171*/172MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),173/**174* Unit that represents the concept of an era.175* The ISO calendar system doesn't have eras thus it is impossible to add176* an era to a date or date-time.177* The estimated duration of the era is artificially defined as {@code 1,000,000,000 Years}.178* <p>179* When used with other calendar systems there are no restrictions on the unit.180*/181ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),182/**183* Artificial unit that represents the concept of forever.184* This is primarily used with {@link TemporalField} to represent unbounded fields185* such as the year or era.186* The estimated duration of this unit is artificially defined as the largest duration187* supported by {@link Duration}.188*/189FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));190191private final String name;192private final Duration duration;193194private ChronoUnit(String name, Duration estimatedDuration) {195this.name = name;196this.duration = estimatedDuration;197}198199//-----------------------------------------------------------------------200/**201* Gets the estimated duration of this unit in the ISO calendar system.202* <p>203* All of the units in this class have an estimated duration.204* Days vary due to daylight saving time, while months have different lengths.205*206* @return the estimated duration of this unit, not null207*/208@Override209public Duration getDuration() {210return duration;211}212213/**214* Checks if the duration of the unit is an estimate.215* <p>216* All time units in this class are considered to be accurate, while all date217* units in this class are considered to be estimated.218* <p>219* This definition ignores leap seconds, but considers that Days vary due to220* daylight saving time and months have different lengths.221*222* @return true if the duration is estimated, false if accurate223*/224@Override225public boolean isDurationEstimated() {226return this.compareTo(DAYS) >= 0;227}228229//-----------------------------------------------------------------------230/**231* Checks if this unit is a date unit.232* <p>233* All units from days to eras inclusive are date-based.234* Time-based units and {@code FOREVER} return false.235*236* @return true if a date unit, false if a time unit237*/238@Override239public boolean isDateBased() {240return this.compareTo(DAYS) >= 0 && this != FOREVER;241}242243/**244* Checks if this unit is a time unit.245* <p>246* All units from nanos to half-days inclusive are time-based.247* Date-based units and {@code FOREVER} return false.248*249* @return true if a time unit, false if a date unit250*/251@Override252public boolean isTimeBased() {253return this.compareTo(DAYS) < 0;254}255256//-----------------------------------------------------------------------257@Override258public boolean isSupportedBy(Temporal temporal) {259return temporal.isSupported(this);260}261262@SuppressWarnings("unchecked")263@Override264public <R extends Temporal> R addTo(R temporal, long amount) {265return (R) temporal.plus(amount, this);266}267268//-----------------------------------------------------------------------269@Override270public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {271return temporal1Inclusive.until(temporal2Exclusive, this);272}273274//-----------------------------------------------------------------------275@Override276public String toString() {277return name;278}279280}281282283