jdk/src/share/classes/java/time/temporal/ChronoUnit.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
package java.time.temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
import java.time.Duration;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    60
import java.time.chrono.ChronoLocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    61
import java.time.chrono.ChronoLocalDateTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    62
import java.time.chrono.ChronoZonedDateTime;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
 * A standard set of date periods units.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
 * This set of units provide unit-based access to manipulate a date, time or date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
 * The standard set of units can be extended by implementing {@link TemporalUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
 * These units are intended to be applicable in multiple calendar systems.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
 * For example, most non-ISO calendar systems define units of years, months and days,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
 * just with slightly different rules.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
 * The documentation of each unit explains how it operates.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
 * This is a final, immutable and thread-safe enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
public enum ChronoUnit implements TemporalUnit {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
     * Unit that represents the concept of a nanosecond, the smallest supported unit of time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
     * For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
    NANOS("Nanos", Duration.ofNanos(1)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
     * Unit that represents the concept of a microsecond.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
     * For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
    MICROS("Micros", Duration.ofNanos(1000)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
     * Unit that represents the concept of a millisecond.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
     * For the ISO calendar system, it is equal to the 1000th part of the second unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
    MILLIS("Millis", Duration.ofNanos(1000_000)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
     * Unit that represents the concept of a second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
     * For the ISO calendar system, it is equal to the second in the SI system
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
     * of units, except around a leap-second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
    SECONDS("Seconds", Duration.ofSeconds(1)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
     * Unit that represents the concept of a minute.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
     * For the ISO calendar system, it is equal to 60 seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
    MINUTES("Minutes", Duration.ofSeconds(60)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
     * Unit that represents the concept of an hour.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
     * For the ISO calendar system, it is equal to 60 minutes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
    HOURS("Hours", Duration.ofSeconds(3600)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
     * Unit that represents the concept of half a day, as used in AM/PM.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
     * For the ISO calendar system, it is equal to 12 hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
    HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     * Unit that represents the concept of a day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     * For the ISO calendar system, it is the standard day from midnight to midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     * The estimated duration of a day is {@code 24 Hours}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * When used with other calendar systems it must correspond to the day defined by
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     * the rising and setting of the Sun on Earth. It is not required that days begin
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
     * at midnight - when converting between calendar systems, the date should be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
     * equivalent at midday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
    DAYS("Days", Duration.ofSeconds(86400)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     * Unit that represents the concept of a week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * For the ISO calendar system, it is equal to 7 days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * When used with other calendar systems it must correspond to an integral number of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
    WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * Unit that represents the concept of a month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * For the ISO calendar system, the length of the month varies by month-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * The estimated duration of a month is one twelfth of {@code 365.2425 Days}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * When used with other calendar systems it must correspond to an integral number of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
    MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
     * Unit that represents the concept of a year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * For the ISO calendar system, it is equal to 12 months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * The estimated duration of a year is {@code 365.2425 Days}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
     * When used with other calendar systems it must correspond to an integral number of days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     * or months roughly equal to a year defined by the passage of the Earth around the Sun.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
    YEARS("Years", Duration.ofSeconds(31556952L)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     * Unit that represents the concept of a decade.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * For the ISO calendar system, it is equal to 10 years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     * When used with other calendar systems it must correspond to an integral number of days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * and is normally an integral number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
    DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * Unit that represents the concept of a century.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * For the ISO calendar system, it is equal to 100 years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * When used with other calendar systems it must correspond to an integral number of days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * and is normally an integral number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
    CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     * Unit that represents the concept of a millennium.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     * For the ISO calendar system, it is equal to 1000 years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     * When used with other calendar systems it must correspond to an integral number of days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
     * and is normally an integral number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
    MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * Unit that represents the concept of an era.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * The ISO calendar system doesn't have eras thus it is impossible to add
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * an era to a date or date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     * The estimated duration of the era is artificially defined as {@code 1,000,00,000 Years}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * When used with other calendar systems there are no restrictions on the unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
    ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
     * Artificial unit that represents the concept of forever.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     * This is primarily used with {@link TemporalField} to represent unbounded fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * such as the year or era.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
     * The estimated duration of the era is artificially defined as the largest duration
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     * supported by {@code Duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
    FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
    private final String name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
    private final Duration duration;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
    private ChronoUnit(String name, Duration estimatedDuration) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
        this.name = name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
        this.duration = estimatedDuration;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
    public String getName() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
        return name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * Gets the estimated duration of this unit in the ISO calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * All of the units in this class have an estimated duration.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * Days vary due to daylight saving time, while months have different lengths.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * @return the estimated duration of this unit, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
    public Duration getDuration() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
        return duration;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * Checks if the duration of the unit is an estimate.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * All time units in this class are considered to be accurate, while all date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * units in this class are considered to be estimated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * This definition ignores leap seconds, but considers that Days vary due to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * daylight saving time and months have different lengths.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * @return true if the duration is estimated, false if accurate
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
    public boolean isDurationEstimated() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
        return isDateUnit();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * Checks if this unit is a date unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * @return true if a date unit, false if a time unit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
    public boolean isDateUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
        return this.compareTo(DAYS) >= 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     * Checks if this unit is a time unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * @return true if a time unit, false if a date unit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
    public boolean isTimeUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
        return this.compareTo(DAYS) < 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   260
    public boolean isSupportedBy(Temporal temporal) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
        if (this == FOREVER) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
            return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
        if (temporal instanceof ChronoLocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
            return isDateUnit();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
        if (temporal instanceof ChronoLocalDateTime || temporal instanceof ChronoZonedDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   270
        return TemporalUnit.super.isSupportedBy(temporal);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   275
    public <R extends Temporal> R addTo(R temporal, long amount) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
        return (R) temporal.plus(amount, this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   281
    public long between(Temporal temporal1, Temporal temporal2) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
        return temporal1.periodUntil(temporal2, this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
        return getName();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
}