src/java.base/share/classes/java/time/Year.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 52078 4a63197816ce
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     4
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    10
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    15
 * accompanied this code).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    16
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    20
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    23
 * questions.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    24
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    25
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    26
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    30
 * file:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    31
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    33
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    34
 * All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    35
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    38
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    41
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    45
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    49
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    61
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    62
package java.time;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    63
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.ERA;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.YEAR;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    67
import static java.time.temporal.ChronoUnit.CENTURIES;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    68
import static java.time.temporal.ChronoUnit.DECADES;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    69
import static java.time.temporal.ChronoUnit.ERAS;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    70
import static java.time.temporal.ChronoUnit.MILLENNIA;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    71
import static java.time.temporal.ChronoUnit.YEARS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    72
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    73
import java.io.DataInput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    74
import java.io.DataOutput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    75
import java.io.IOException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    76
import java.io.InvalidObjectException;
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20795
diff changeset
    77
import java.io.ObjectInputStream;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    78
import java.io.Serializable;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    79
import java.time.chrono.Chronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    80
import java.time.chrono.IsoChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    81
import java.time.format.DateTimeFormatter;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    82
import java.time.format.DateTimeFormatterBuilder;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    83
import java.time.format.DateTimeParseException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    84
import java.time.format.SignStyle;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    85
import java.time.temporal.ChronoField;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    86
import java.time.temporal.ChronoUnit;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    87
import java.time.temporal.Temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    88
import java.time.temporal.TemporalAccessor;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    89
import java.time.temporal.TemporalAdjuster;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    90
import java.time.temporal.TemporalAmount;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    91
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
    92
import java.time.temporal.TemporalQueries;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    93
import java.time.temporal.TemporalQuery;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    94
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    95
import java.time.temporal.UnsupportedTemporalTypeException;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    96
import java.time.temporal.ValueRange;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    97
import java.util.Objects;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    98
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    99
/**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   100
 * A year in the ISO-8601 calendar system, such as {@code 2007}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   101
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   102
 * {@code Year} is an immutable date-time object that represents a year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   103
 * Any field that can be derived from a year can be obtained.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   104
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   105
 * <b>Note that years in the ISO chronology only align with years in the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   106
 * Gregorian-Julian system for modern years. Parts of Russia did not switch to the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   107
 * modern Gregorian/ISO rules until 1920.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   108
 * As such, historical years must be treated with caution.</b>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   109
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   110
 * This class does not store or represent a month, day, time or time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   111
 * For example, the value "2007" can be stored in a {@code Year}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   112
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   113
 * Years represented by this class follow the ISO-8601 standard and use
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   114
 * the proleptic numbering system. Year 1 is preceded by year 0, then by year -1.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   115
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   116
 * The ISO-8601 calendar system is the modern civil calendar system used today
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   117
 * in most of the world. It is equivalent to the proleptic Gregorian calendar
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   118
 * system, in which today's rules for leap years are applied for all time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   119
 * For most applications written today, the ISO-8601 rules are entirely suitable.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   120
 * However, any application that makes use of historical dates, and requires them
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   121
 * to be accurate will find the ISO-8601 approach unsuitable.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   122
 *
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   123
 * <p>
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47216
diff changeset
   124
 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   125
 * class; use of identity-sensitive operations (including reference equality
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   126
 * ({@code ==}), identity hash code, or synchronization) on instances of
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   127
 * {@code Year} may have unpredictable results and should be avoided.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   128
 * The {@code equals} method should be used for comparisons.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   129
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   130
 * @implSpec
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   131
 * This class is immutable and thread-safe.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   132
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   133
 * @since 1.8
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   134
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   135
public final class Year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   136
        implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   137
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   138
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   139
     * The minimum supported year, '-999,999,999'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   140
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   141
    public static final int MIN_VALUE = -999_999_999;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   142
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   143
     * The maximum supported year, '+999,999,999'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   144
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   145
    public static final int MAX_VALUE = 999_999_999;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   146
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   147
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   148
     * Serialization version.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   149
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
   150
    @java.io.Serial
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   151
    private static final long serialVersionUID = -23038383694477807L;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   152
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   153
     * Parser.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   154
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   155
    private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   156
        .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   157
        .toFormatter();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   158
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   159
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   160
     * The year being represented.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   161
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   162
    private final int year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   163
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   164
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   165
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   166
     * Obtains the current year from the system clock in the default time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   167
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   168
     * This will query the {@link Clock#systemDefaultZone() system clock} in the default
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   169
     * time-zone to obtain the current year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   170
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   171
     * Using this method will prevent the ability to use an alternate clock for testing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   172
     * because the clock is hard-coded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   173
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   174
     * @return the current year using the system clock and default time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   175
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   176
    public static Year now() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   177
        return now(Clock.systemDefaultZone());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   178
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   179
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   180
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   181
     * Obtains the current year from the system clock in the specified time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   182
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   183
     * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current year.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   184
     * Specifying the time-zone avoids dependence on the default time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   185
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   186
     * Using this method will prevent the ability to use an alternate clock for testing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   187
     * because the clock is hard-coded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   188
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   189
     * @param zone  the zone ID to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   190
     * @return the current year using the system clock, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   191
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   192
    public static Year now(ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   193
        return now(Clock.system(zone));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   194
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   195
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   196
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   197
     * Obtains the current year from the specified clock.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   198
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   199
     * This will query the specified clock to obtain the current year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   200
     * Using this method allows the use of an alternate clock for testing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   201
     * The alternate clock may be introduced using {@link Clock dependency injection}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   202
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   203
     * @param clock  the clock to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   204
     * @return the current year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   205
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   206
    public static Year now(Clock clock) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   207
        final LocalDate now = LocalDate.now(clock);  // called once
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   208
        return Year.of(now.getYear());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   209
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   210
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   211
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   212
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   213
     * Obtains an instance of {@code Year}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   214
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   215
     * This method accepts a year value from the proleptic ISO calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   216
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   217
     * The year 2AD/CE is represented by 2.<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   218
     * The year 1AD/CE is represented by 1.<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   219
     * The year 1BC/BCE is represented by 0.<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   220
     * The year 2BC/BCE is represented by -1.<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   221
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   222
     * @param isoYear  the ISO proleptic year to represent, from {@code MIN_VALUE} to {@code MAX_VALUE}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   223
     * @return the year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   224
     * @throws DateTimeException if the field is invalid
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   225
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   226
    public static Year of(int isoYear) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   227
        YEAR.checkValidValue(isoYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   228
        return new Year(isoYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   229
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   230
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   231
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   232
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   233
     * Obtains an instance of {@code Year} from a temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   234
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   235
     * This obtains a year based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   236
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   237
     * which this factory converts to an instance of {@code Year}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   238
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   239
     * The conversion extracts the {@link ChronoField#YEAR year} field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   240
     * The extraction is only permitted if the temporal object has an ISO
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   241
     * chronology, or can be converted to a {@code LocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   242
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   243
     * This method matches the signature of the functional interface {@link TemporalQuery}
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   244
     * allowing it to be used as a query via method reference, {@code Year::from}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   245
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   246
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   247
     * @return the year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   248
     * @throws DateTimeException if unable to convert to a {@code Year}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   249
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   250
    public static Year from(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   251
        if (temporal instanceof Year) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   252
            return (Year) temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   253
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   254
        Objects.requireNonNull(temporal, "temporal");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   255
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   256
            if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   257
                temporal = LocalDate.from(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   258
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   259
            return of(temporal.get(YEAR));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   260
        } catch (DateTimeException ex) {
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   261
            throw new DateTimeException("Unable to obtain Year from TemporalAccessor: " +
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   262
                    temporal + " of type " + temporal.getClass().getName(), ex);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   263
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   264
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   265
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   266
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   267
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   268
     * Obtains an instance of {@code Year} from a text string such as {@code 2007}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   269
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   270
     * The string must represent a valid year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   271
     * Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   272
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   273
     * @param text  the text to parse such as "2007", not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   274
     * @return the parsed year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   275
     * @throws DateTimeParseException if the text cannot be parsed
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   276
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   277
    public static Year parse(CharSequence text) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   278
        return parse(text, PARSER);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   279
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   280
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   281
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   282
     * Obtains an instance of {@code Year} from a text string using a specific formatter.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   283
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   284
     * The text is parsed using the formatter, returning a year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   285
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   286
     * @param text  the text to parse, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   287
     * @param formatter  the formatter to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   288
     * @return the parsed year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   289
     * @throws DateTimeParseException if the text cannot be parsed
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   290
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   291
    public static Year parse(CharSequence text, DateTimeFormatter formatter) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   292
        Objects.requireNonNull(formatter, "formatter");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   293
        return formatter.parse(text, Year::from);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   294
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   295
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   296
    //-------------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   297
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   298
     * Checks if the year is a leap year, according to the ISO proleptic
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   299
     * calendar system rules.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   300
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   301
     * This method applies the current rules for leap years across the whole time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   302
     * In general, a year is a leap year if it is divisible by four without
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   303
     * remainder. However, years divisible by 100, are not leap years, with
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   304
     * the exception of years divisible by 400 which are.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   305
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   306
     * For example, 1904 is a leap year it is divisible by 4.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   307
     * 1900 was not a leap year as it is divisible by 100, however 2000 was a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   308
     * leap year as it is divisible by 400.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   309
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   310
     * The calculation is proleptic - applying the same rules into the far future and far past.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   311
     * This is historically inaccurate, but is correct for the ISO-8601 standard.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   312
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   313
     * @param year  the year to check
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   314
     * @return true if the year is leap, false otherwise
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   315
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   316
    public static boolean isLeap(long year) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   317
        return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   318
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   319
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   320
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   321
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   322
     * Constructor.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   323
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   324
     * @param year  the year to represent
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   325
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   326
    private Year(int year) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   327
        this.year = year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   328
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   329
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   330
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   331
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   332
     * Gets the year value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   333
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   334
     * The year returned by this method is proleptic as per {@code get(YEAR)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   335
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   336
     * @return the year, {@code MIN_VALUE} to {@code MAX_VALUE}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   337
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   338
    public int getValue() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   339
        return year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   340
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   341
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   342
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   343
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   344
     * Checks if the specified field is supported.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   345
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   346
     * This checks if this year can be queried for the specified field.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   347
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   348
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   349
     * methods will throw an exception.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   350
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   351
     * If the field is a {@link ChronoField} then the query is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   352
     * The supported fields are:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   353
     * <ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   354
     * <li>{@code YEAR_OF_ERA}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   355
     * <li>{@code YEAR}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   356
     * <li>{@code ERA}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   357
     * </ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   358
     * All other {@code ChronoField} instances will return false.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   359
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   360
     * If the field is not a {@code ChronoField}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   361
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   362
     * passing {@code this} as the argument.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   363
     * Whether the field is supported is determined by the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   364
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   365
     * @param field  the field to check, null returns false
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   366
     * @return true if the field is supported on this year, false if not
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   367
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   368
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   369
    public boolean isSupported(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   370
        if (field instanceof ChronoField) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   371
            return field == YEAR || field == YEAR_OF_ERA || field == ERA;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   372
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   373
        return field != null && field.isSupportedBy(this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   374
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   375
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   376
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   377
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   378
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   379
     * This checks if the specified unit can be added to, or subtracted from, this year.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   380
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   381
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   382
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   383
     * If the unit is a {@link ChronoUnit} then the query is implemented here.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   384
     * The supported units are:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   385
     * <ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   386
     * <li>{@code YEARS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   387
     * <li>{@code DECADES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   388
     * <li>{@code CENTURIES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   389
     * <li>{@code MILLENNIA}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   390
     * <li>{@code ERAS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   391
     * </ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   392
     * All other {@code ChronoUnit} instances will return false.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   393
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   394
     * If the unit is not a {@code ChronoUnit}, then the result of this method
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   395
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   396
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   397
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   398
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   399
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   400
     * @return true if the unit can be added/subtracted, false if not
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   401
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   402
    @Override
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   403
    public boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   404
        if (unit instanceof ChronoUnit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   405
            return unit == YEARS || unit == DECADES || unit == CENTURIES || unit == MILLENNIA || unit == ERAS;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   406
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   407
        return unit != null && unit.isSupportedBy(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   408
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   409
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   410
    //-----------------------------------------------------------------------
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   411
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   412
     * Gets the range of valid values for the specified field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   413
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   414
     * The range object expresses the minimum and maximum valid values for a field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   415
     * This year is used to enhance the accuracy of the returned range.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   416
     * If it is not possible to return the range, because the field is not supported
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   417
     * or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   418
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   419
     * If the field is a {@link ChronoField} then the query is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   420
     * The {@link #isSupported(TemporalField) supported fields} will return
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   421
     * appropriate range instances.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   422
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   423
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   424
     * If the field is not a {@code ChronoField}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   425
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   426
     * passing {@code this} as the argument.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   427
     * Whether the range can be obtained is determined by the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   428
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   429
     * @param field  the field to query the range for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   430
     * @return the range of valid values for the field, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   431
     * @throws DateTimeException if the range for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   432
     * @throws UnsupportedTemporalTypeException if the field is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   433
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   434
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   435
    public ValueRange range(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   436
        if (field == YEAR_OF_ERA) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   437
            return (year <= 0 ? ValueRange.of(1, MAX_VALUE + 1) : ValueRange.of(1, MAX_VALUE));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   438
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   439
        return Temporal.super.range(field);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   440
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   441
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   442
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   443
     * Gets the value of the specified field from this year as an {@code int}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   444
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   445
     * This queries this year for the value of the specified field.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   446
     * The returned value will always be within the valid range of values for the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   447
     * If it is not possible to return the value, because the field is not supported
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   448
     * or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   449
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   450
     * If the field is a {@link ChronoField} then the query is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   451
     * The {@link #isSupported(TemporalField) supported fields} will return valid
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   452
     * values based on this year.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   453
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   454
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   455
     * If the field is not a {@code ChronoField}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   456
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   457
     * passing {@code this} as the argument. Whether the value can be obtained,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   458
     * and what the value represents, is determined by the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   459
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   460
     * @param field  the field to get, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   461
     * @return the value for the field
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   462
     * @throws DateTimeException if a value for the field cannot be obtained or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   463
     *         the value is outside the range of valid values for the field
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   464
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   465
     *         the range of values exceeds an {@code int}
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   466
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   467
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   468
    @Override  // override for Javadoc
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   469
    public int get(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   470
        return range(field).checkValidIntValue(getLong(field), field);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   471
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   472
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   473
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   474
     * Gets the value of the specified field from this year as a {@code long}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   475
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   476
     * This queries this year for the value of the specified field.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   477
     * If it is not possible to return the value, because the field is not supported
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   478
     * or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   479
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   480
     * If the field is a {@link ChronoField} then the query is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   481
     * The {@link #isSupported(TemporalField) supported fields} will return valid
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   482
     * values based on this year.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   483
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   484
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   485
     * If the field is not a {@code ChronoField}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   486
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   487
     * passing {@code this} as the argument. Whether the value can be obtained,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   488
     * and what the value represents, is determined by the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   489
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   490
     * @param field  the field to get, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   491
     * @return the value for the field
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   492
     * @throws DateTimeException if a value for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   493
     * @throws UnsupportedTemporalTypeException if the field is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   494
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   495
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   496
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   497
    public long getLong(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   498
        if (field instanceof ChronoField) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   499
            switch ((ChronoField) field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   500
                case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   501
                case YEAR: return year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   502
                case ERA: return (year < 1 ? 0 : 1);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   503
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   504
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   505
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   506
        return field.getFrom(this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   507
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   508
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   509
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   510
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   511
     * Checks if the year is a leap year, according to the ISO proleptic
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   512
     * calendar system rules.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   513
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   514
     * This method applies the current rules for leap years across the whole time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   515
     * In general, a year is a leap year if it is divisible by four without
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   516
     * remainder. However, years divisible by 100, are not leap years, with
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   517
     * the exception of years divisible by 400 which are.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   518
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   519
     * For example, 1904 is a leap year it is divisible by 4.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   520
     * 1900 was not a leap year as it is divisible by 100, however 2000 was a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   521
     * leap year as it is divisible by 400.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   522
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   523
     * The calculation is proleptic - applying the same rules into the far future and far past.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   524
     * This is historically inaccurate, but is correct for the ISO-8601 standard.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   525
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   526
     * @return true if the year is leap, false otherwise
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   527
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   528
    public boolean isLeap() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   529
        return Year.isLeap(year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   530
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   531
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   532
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   533
     * Checks if the month-day is valid for this year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   534
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   535
     * This method checks whether this year and the input month and day form
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   536
     * a valid date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   537
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   538
     * @param monthDay  the month-day to validate, null returns false
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   539
     * @return true if the month and day are valid for this year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   540
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   541
    public boolean isValidMonthDay(MonthDay monthDay) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   542
        return monthDay != null && monthDay.isValidYear(year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   543
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   544
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   545
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   546
     * Gets the length of this year in days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   547
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   548
     * @return the length of this year in days, 365 or 366
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   549
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   550
    public int length() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   551
        return isLeap() ? 366 : 365;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   552
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   553
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   554
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   555
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   556
     * Returns an adjusted copy of this year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   557
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   558
     * This returns a {@code Year}, based on this one, with the year adjusted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   559
     * The adjustment takes place using the specified adjuster strategy object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   560
     * Read the documentation of the adjuster to understand what adjustment will be made.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   561
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   562
     * The result of this method is obtained by invoking the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   563
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   564
     * specified adjuster passing {@code this} as the argument.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   565
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   566
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   567
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   568
     * @param adjuster the adjuster to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   569
     * @return a {@code Year} based on {@code this} with the adjustment made, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   570
     * @throws DateTimeException if the adjustment cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   571
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   572
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   573
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   574
    public Year with(TemporalAdjuster adjuster) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   575
        return (Year) adjuster.adjustInto(this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   576
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   577
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   578
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   579
     * Returns a copy of this year with the specified field set to a new value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   580
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   581
     * This returns a {@code Year}, based on this one, with the value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   582
     * for the specified field changed.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   583
     * If it is not possible to set the value, because the field is not supported or for
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   584
     * some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   585
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   586
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   587
     * The supported fields behave as follows:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   588
     * <ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   589
     * <li>{@code YEAR_OF_ERA} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   590
     *  Returns a {@code Year} with the specified year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   591
     *  The era will be unchanged.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   592
     * <li>{@code YEAR} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   593
     *  Returns a {@code Year} with the specified year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   594
     *  This completely replaces the date and is equivalent to {@link #of(int)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   595
     * <li>{@code ERA} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   596
     *  Returns a {@code Year} with the specified era.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   597
     *  The year-of-era will be unchanged.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   598
     * </ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   599
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   600
     * In all cases, if the new value is outside the valid range of values for the field
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   601
     * then a {@code DateTimeException} will be thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   602
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   603
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   604
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   605
     * If the field is not a {@code ChronoField}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   606
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   607
     * passing {@code this} as the argument. In this case, the field determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   608
     * whether and how to adjust the instant.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   609
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   610
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   611
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   612
     * @param field  the field to set in the result, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   613
     * @param newValue  the new value of the field in the result
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   614
     * @return a {@code Year} based on {@code this} with the specified field set, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   615
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   616
     * @throws UnsupportedTemporalTypeException if the field is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   617
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   618
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   619
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   620
    public Year with(TemporalField field, long newValue) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   621
        if (field instanceof ChronoField) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   622
            ChronoField f = (ChronoField) field;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   623
            f.checkValidValue(newValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   624
            switch (f) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   625
                case YEAR_OF_ERA: return Year.of((int) (year < 1 ? 1 - newValue : newValue));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   626
                case YEAR: return Year.of((int) newValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   627
                case ERA: return (getLong(ERA) == newValue ? this : Year.of(1 - year));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   628
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   629
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   630
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   631
        return field.adjustInto(this, newValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   632
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   633
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   634
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   635
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   636
     * Returns a copy of this year with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   637
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   638
     * This returns a {@code Year}, based on this one, with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   639
     * The amount is typically {@link Period} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   640
     * the {@link TemporalAmount} interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   641
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   642
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   643
     * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   644
     * to implement the addition in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   645
     * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   646
     * of the amount implementation to determine if it can be successfully added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   647
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   648
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   649
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   650
     * @param amountToAdd  the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   651
     * @return a {@code Year} based on this year with the addition made, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   652
     * @throws DateTimeException if the addition cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   653
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   654
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   655
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   656
    public Year plus(TemporalAmount amountToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   657
        return (Year) amountToAdd.addTo(this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   658
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   659
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   660
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   661
     * Returns a copy of this year with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   662
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   663
     * This returns a {@code Year}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   664
     * in terms of the unit added. If it is not possible to add the amount, because the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   665
     * unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   666
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   667
     * If the field is a {@link ChronoUnit} then the addition is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   668
     * The supported fields behave as follows:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   669
     * <ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   670
     * <li>{@code YEARS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   671
     *  Returns a {@code Year} with the specified number of years added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   672
     *  This is equivalent to {@link #plusYears(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   673
     * <li>{@code DECADES} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   674
     *  Returns a {@code Year} with the specified number of decades added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   675
     *  This is equivalent to calling {@link #plusYears(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   676
     *  multiplied by 10.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   677
     * <li>{@code CENTURIES} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   678
     *  Returns a {@code Year} with the specified number of centuries added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   679
     *  This is equivalent to calling {@link #plusYears(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   680
     *  multiplied by 100.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   681
     * <li>{@code MILLENNIA} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   682
     *  Returns a {@code Year} with the specified number of millennia added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   683
     *  This is equivalent to calling {@link #plusYears(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   684
     *  multiplied by 1,000.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   685
     * <li>{@code ERAS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   686
     *  Returns a {@code Year} with the specified number of eras added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   687
     *  Only two eras are supported so the amount must be one, zero or minus one.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   688
     *  If the amount is non-zero then the year is changed such that the year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   689
     *  is unchanged.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   690
     * </ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   691
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   692
     * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   693
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   694
     * If the field is not a {@code ChronoUnit}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   695
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   696
     * passing {@code this} as the argument. In this case, the unit determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   697
     * whether and how to perform the addition.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   698
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   699
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   700
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   701
     * @param amountToAdd  the amount of the unit to add to the result, may be negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   702
     * @param unit  the unit of the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   703
     * @return a {@code Year} based on this year with the specified amount added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   704
     * @throws DateTimeException if the addition cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   705
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   706
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   707
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   708
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   709
    public Year plus(long amountToAdd, TemporalUnit unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   710
        if (unit instanceof ChronoUnit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   711
            switch ((ChronoUnit) unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   712
                case YEARS: return plusYears(amountToAdd);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   713
                case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   714
                case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   715
                case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   716
                case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   717
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   718
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   719
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   720
        return unit.addTo(this, amountToAdd);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   721
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   722
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   723
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   724
     * Returns a copy of this {@code Year} with the specified number of years added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   725
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   726
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   727
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   728
     * @param yearsToAdd  the years to add, may be negative
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   729
     * @return a {@code Year} based on this year with the years added, not null
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   730
     * @throws DateTimeException if the result exceeds the supported range
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   731
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   732
    public Year plusYears(long yearsToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   733
        if (yearsToAdd == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   734
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   735
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   736
        return of(YEAR.checkValidIntValue(year + yearsToAdd));  // overflow safe
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   737
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   738
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   739
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   740
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   741
     * Returns a copy of this year with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   742
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   743
     * This returns a {@code Year}, based on this one, with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   744
     * The amount is typically {@link Period} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   745
     * the {@link TemporalAmount} interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   746
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   747
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   748
     * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   749
     * to implement the subtraction in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   750
     * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   751
     * of the amount implementation to determine if it can be successfully subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   752
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   753
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   754
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   755
     * @param amountToSubtract  the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   756
     * @return a {@code Year} based on this year with the subtraction made, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   757
     * @throws DateTimeException if the subtraction cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   758
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   759
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   760
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   761
    public Year minus(TemporalAmount amountToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   762
        return (Year) amountToSubtract.subtractFrom(this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   763
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   764
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   765
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   766
     * Returns a copy of this year with the specified amount subtracted.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   767
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   768
     * This returns a {@code Year}, based on this one, with the amount
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   769
     * in terms of the unit subtracted. If it is not possible to subtract the amount,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   770
     * because the unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   771
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   772
     * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   773
     * See that method for a full description of how addition, and thus subtraction, works.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   774
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   775
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   776
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   777
     * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   778
     * @param unit  the unit of the amount to subtract, not null
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   779
     * @return a {@code Year} based on this year with the specified amount subtracted, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   780
     * @throws DateTimeException if the subtraction cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   781
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   782
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   783
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   784
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   785
    public Year minus(long amountToSubtract, TemporalUnit unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   786
        return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   787
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   788
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   789
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   790
     * Returns a copy of this {@code Year} with the specified number of years subtracted.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   791
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   792
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   793
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   794
     * @param yearsToSubtract  the years to subtract, may be negative
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   795
     * @return a {@code Year} based on this year with the year subtracted, not null
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   796
     * @throws DateTimeException if the result exceeds the supported range
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   797
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   798
    public Year minusYears(long yearsToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   799
        return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   800
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   801
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   802
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   803
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   804
     * Queries this year using the specified query.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   805
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   806
     * This queries this year using the specified query strategy object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   807
     * The {@code TemporalQuery} object defines the logic to be used to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   808
     * obtain the result. Read the documentation of the query to understand
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   809
     * what the result of this method will be.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   810
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   811
     * The result of this method is obtained by invoking the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   812
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   813
     * specified query passing {@code this} as the argument.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   814
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   815
     * @param <R> the type of the result
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   816
     * @param query  the query to invoke, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   817
     * @return the query result, null may be returned (defined by the query)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   818
     * @throws DateTimeException if unable to query (defined by the query)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   819
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   820
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   821
    @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   822
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   823
    public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
   824
        if (query == TemporalQueries.chronology()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   825
            return (R) IsoChronology.INSTANCE;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
   826
        } else if (query == TemporalQueries.precision()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   827
            return (R) YEARS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   828
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   829
        return Temporal.super.query(query);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   830
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   831
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   832
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   833
     * Adjusts the specified temporal object to have this year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   834
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   835
     * This returns a temporal object of the same observable type as the input
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   836
     * with the year changed to be the same as this.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   837
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   838
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   839
     * passing {@link ChronoField#YEAR} as the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   840
     * If the specified temporal object does not use the ISO calendar system then
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   841
     * a {@code DateTimeException} is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   842
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   843
     * In most cases, it is clearer to reverse the calling pattern by using
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   844
     * {@link Temporal#with(TemporalAdjuster)}:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   845
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   846
     *   // these two lines are equivalent, but the second approach is recommended
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   847
     *   temporal = thisYear.adjustInto(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   848
     *   temporal = temporal.with(thisYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   849
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   850
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   851
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   852
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   853
     * @param temporal  the target object to be adjusted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   854
     * @return the adjusted object, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   855
     * @throws DateTimeException if unable to make the adjustment
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   856
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   857
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   858
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   859
    public Temporal adjustInto(Temporal temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   860
        if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   861
            throw new DateTimeException("Adjustment only supported on ISO date-time");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   862
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   863
        return temporal.with(YEAR, year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   864
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   865
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   866
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   867
     * Calculates the amount of time until another year in terms of the specified unit.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   868
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   869
     * This calculates the amount of time between two {@code Year}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   870
     * objects in terms of a single {@code TemporalUnit}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   871
     * The start and end points are {@code this} and the specified year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   872
     * The result will be negative if the end is before the start.
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   873
     * The {@code Temporal} passed to this method is converted to a
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   874
     * {@code Year} using {@link #from(TemporalAccessor)}.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   875
     * For example, the amount in decades between two year can be calculated
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   876
     * using {@code startYear.until(endYear, DECADES)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   877
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   878
     * The calculation returns a whole number, representing the number of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   879
     * complete units between the two years.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   880
     * For example, the amount in decades between 2012 and 2031
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   881
     * will only be one decade as it is one year short of two decades.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   882
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   883
     * There are two equivalent ways of using this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   884
     * The first is to invoke this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   885
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   886
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   887
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   888
     *   amount = start.until(end, YEARS);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   889
     *   amount = YEARS.between(start, end);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   890
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   891
     * The choice should be made based on which makes the code more readable.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   892
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   893
     * The calculation is implemented in this method for {@link ChronoUnit}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   894
     * The units {@code YEARS}, {@code DECADES}, {@code CENTURIES},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   895
     * {@code MILLENNIA} and {@code ERAS} are supported.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   896
     * Other {@code ChronoUnit} values will throw an exception.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   897
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   898
     * If the unit is not a {@code ChronoUnit}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   899
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   900
     * passing {@code this} as the first argument and the converted input temporal
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   901
     * as the second argument.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   902
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   903
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   904
     *
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   905
     * @param endExclusive  the end date, exclusive, which is converted to a {@code Year}, not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   906
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   907
     * @return the amount of time between this year and the end year
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   908
     * @throws DateTimeException if the amount cannot be calculated, or the end
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   909
     *  temporal cannot be converted to a {@code Year}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   910
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   911
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   912
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   913
    @Override
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   914
    public long until(Temporal endExclusive, TemporalUnit unit) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   915
        Year end = Year.from(endExclusive);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   916
        if (unit instanceof ChronoUnit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   917
            long yearsUntil = ((long) end.year) - year;  // no overflow
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   918
            switch ((ChronoUnit) unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   919
                case YEARS: return yearsUntil;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   920
                case DECADES: return yearsUntil / 10;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   921
                case CENTURIES: return yearsUntil / 100;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   922
                case MILLENNIA: return yearsUntil / 1000;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   923
                case ERAS: return end.getLong(ERA) - getLong(ERA);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   924
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   925
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   926
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   927
        return unit.between(this, end);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   928
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   929
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   930
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   931
     * Formats this year using the specified formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   932
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   933
     * This year will be passed to the formatter to produce a string.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   934
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   935
     * @param formatter  the formatter to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   936
     * @return the formatted year string, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   937
     * @throws DateTimeException if an error occurs during printing
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   938
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   939
    public String format(DateTimeFormatter formatter) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   940
        Objects.requireNonNull(formatter, "formatter");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   941
        return formatter.format(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   942
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   943
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   944
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   945
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   946
     * Combines this year with a day-of-year to create a {@code LocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   947
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   948
     * This returns a {@code LocalDate} formed from this year and the specified day-of-year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   949
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   950
     * The day-of-year value 366 is only valid in a leap year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   951
     *
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   952
     * @param dayOfYear  the day-of-year to use, from 1 to 365-366
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   953
     * @return the local date formed from this year and the specified date of year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   954
     * @throws DateTimeException if the day of year is zero or less, 366 or greater or equal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   955
     *  to 366 and this is not a leap year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   956
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   957
    public LocalDate atDay(int dayOfYear) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   958
        return LocalDate.ofYearDay(year, dayOfYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   959
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   960
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   961
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   962
     * Combines this year with a month to create a {@code YearMonth}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   963
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   964
     * This returns a {@code YearMonth} formed from this year and the specified month.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   965
     * All possible combinations of year and month are valid.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   966
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   967
     * This method can be used as part of a chain to produce a date:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   968
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   969
     *  LocalDate date = year.atMonth(month).atDay(day);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   970
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   971
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   972
     * @param month  the month-of-year to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   973
     * @return the year-month formed from this year and the specified month, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   974
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   975
    public YearMonth atMonth(Month month) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   976
        return YearMonth.of(year, month);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   977
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   978
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   979
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   980
     * Combines this year with a month to create a {@code YearMonth}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   981
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   982
     * This returns a {@code YearMonth} formed from this year and the specified month.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   983
     * All possible combinations of year and month are valid.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   984
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   985
     * This method can be used as part of a chain to produce a date:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   986
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   987
     *  LocalDate date = year.atMonth(month).atDay(day);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   988
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   989
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   990
     * @param month  the month-of-year to use, from 1 (January) to 12 (December)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   991
     * @return the year-month formed from this year and the specified month, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   992
     * @throws DateTimeException if the month is invalid
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   993
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   994
    public YearMonth atMonth(int month) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   995
        return YearMonth.of(year, month);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   996
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   997
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   998
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   999
     * Combines this year with a month-day to create a {@code LocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1000
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1001
     * This returns a {@code LocalDate} formed from this year and the specified month-day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1002
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1003
     * A month-day of February 29th will be adjusted to February 28th in the resulting
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1004
     * date if the year is not a leap year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1005
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1006
     * @param monthDay  the month-day to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1007
     * @return the local date formed from this year and the specified month-day, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1008
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1009
    public LocalDate atMonthDay(MonthDay monthDay) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1010
        return monthDay.atYear(year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1011
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1012
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1013
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1014
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1015
     * Compares this year to another year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1016
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1017
     * The comparison is based on the value of the year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1018
     * It is "consistent with equals", as defined by {@link Comparable}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1019
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1020
     * @param other  the other year to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1021
     * @return the comparator value, negative if less, positive if greater
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1022
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1023
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1024
    public int compareTo(Year other) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1025
        return year - other.year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1026
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1027
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1028
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1029
     * Checks if this year is after the specified year.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1030
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1031
     * @param other  the other year to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1032
     * @return true if this is after the specified year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1033
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1034
    public boolean isAfter(Year other) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1035
        return year > other.year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1036
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1037
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1038
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1039
     * Checks if this year is before the specified year.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1040
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1041
     * @param other  the other year to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1042
     * @return true if this point is before the specified year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1043
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1044
    public boolean isBefore(Year other) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1045
        return year < other.year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1046
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1047
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1048
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1049
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1050
     * Checks if this year is equal to another year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1051
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1052
     * The comparison is based on the time-line position of the years.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1053
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1054
     * @param obj  the object to check, null returns false
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1055
     * @return true if this is equal to the other year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1056
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1057
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1058
    public boolean equals(Object obj) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1059
        if (this == obj) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1060
            return true;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1061
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1062
        if (obj instanceof Year) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1063
            return year == ((Year) obj).year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1064
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1065
        return false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1066
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1067
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1068
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1069
     * A hash code for this year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1070
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1071
     * @return a suitable hash code
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1072
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1073
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1074
    public int hashCode() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1075
        return year;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1076
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1077
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1078
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1079
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1080
     * Outputs this year as a {@code String}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1081
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1082
     * @return a string representation of this year, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1083
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1084
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1085
    public String toString() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1086
        return Integer.toString(year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1087
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1088
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1089
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1090
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1091
     * Writes the object using a
52078
4a63197816ce 8211952: Broken links in java.time API
jjg
parents: 49433
diff changeset
  1092
     * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1093
     * @serialData
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1094
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1095
     *  out.writeByte(11);  // identifies a Year
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1096
     *  out.writeInt(year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1097
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1098
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1099
     * @return the instance of {@code Ser}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1100
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
  1101
    @java.io.Serial
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1102
    private Object writeReplace() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1103
        return new Ser(Ser.YEAR_TYPE, this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1104
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1105
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1106
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1107
     * Defend against malicious streams.
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20795
diff changeset
  1108
     *
22566
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22108
diff changeset
  1109
     * @param s the stream to read
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1110
     * @throws InvalidObjectException always
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1111
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52078
diff changeset
  1112
    @java.io.Serial
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20795
diff changeset
  1113
    private void readObject(ObjectInputStream s) throws InvalidObjectException {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1114
        throw new InvalidObjectException("Deserialization via serialization delegate");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1115
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1116
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1117
    void writeExternal(DataOutput out) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1118
        out.writeInt(year);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1119
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1120
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1121
    static Year readExternal(DataInput in) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1122
        return Year.of(in.readInt());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1123
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1124
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
  1125
}