jdk/src/java.base/share/classes/java/time/LocalDate.java
author rriggs
Mon, 16 Nov 2015 15:28:55 -0500
changeset 33831 022f746e1d74
parent 25859 3317bb8137f4
child 33847 7857fc64d0c0
permissions -rw-r--r--
8133079: java.time LocalDate and LocalTime ofInstant() factory methods Reviewed-by: rriggs, scolebourne
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
33831
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.LocalTime.SECONDS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import static java.time.temporal.ChronoField.EPOCH_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import static java.time.temporal.ChronoField.ERA;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    74
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import static java.time.temporal.ChronoField.YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.io.DataInput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.io.DataOutput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.io.IOException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.io.InvalidObjectException;
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20795
diff changeset
    81
import java.io.ObjectInputStream;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    83
import java.time.chrono.ChronoLocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    84
import java.time.chrono.Era;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    85
import java.time.chrono.IsoChronology;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.format.DateTimeFormatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
import java.time.temporal.TemporalAdjuster;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    93
import java.time.temporal.TemporalAmount;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
    95
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    98
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
import java.time.zone.ZoneOffsetTransition;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
import java.time.zone.ZoneRules;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * A date without a time-zone in the ISO-8601 calendar system,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * such as {@code 2007-12-03}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 * {@code LocalDate} is an immutable date-time object that represents a date,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 * often viewed as year-month-day. Other date fields, such as day-of-year,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 * day-of-week and week-of-year, can also be accessed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
 * For example, the value "2nd October 2007" can be stored in a {@code LocalDate}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 * This class does not store or represent a time or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 * Instead, it is a description of the date, as used for birthdays.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * It cannot represent an instant on the time-line without additional information
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
 * such as an offset or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 * The ISO-8601 calendar system is the modern civil calendar system used today
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 * in most of the world. It is equivalent to the proleptic Gregorian calendar
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 * system, in which today's rules for leap years are applied for all time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 * For most applications written today, the ISO-8601 rules are entirely suitable.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
 * However, any application that makes use of historical dates, and requires them
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
 * to be accurate will find the ISO-8601 approach unsuitable.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 *
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   125
 * <p>
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   126
 * This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a>
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   127
 * class; use of identity-sensitive operations (including reference equality
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   128
 * ({@code ==}), identity hash code, or synchronization) on instances of
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   129
 * {@code LocalDate} may have unpredictable results and should be avoided.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   130
 * The {@code equals} method should be used for comparisons.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   131
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   132
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
public final class LocalDate
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   138
        implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * The minimum supported {@code LocalDate}, '-999999999-01-01'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * This could be used by an application as a "far past" date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * The maximum supported {@code LocalDate}, '+999999999-12-31'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * This could be used by an application as a "far future" date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
    public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
    private static final long serialVersionUID = 2942565459149668126L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * The number of days in a 400 year cycle.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
    private static final int DAYS_PER_CYCLE = 146097;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * The number of days from year zero to year 1970.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * There are five 400 year cycles from year zero to 2000.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * There are 7 leap years from 1970 to 2000.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
    static final long DAYS_0000_TO_1970 = (DAYS_PER_CYCLE * 5L) - (30L * 365L + 7L);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     * The year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    private final int year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     * The month-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
    private final short month;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     * The day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
    private final short day;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     * Obtains the current date from the system clock in the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * This will query the {@link Clock#systemDefaultZone() system clock} in the default
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * time-zone to obtain the current date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     * Using this method will prevent the ability to use an alternate clock for testing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * @return the current date using the system clock and default time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
    public static LocalDate now() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
        return now(Clock.systemDefaultZone());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * Obtains the current date from the system clock in the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     * Specifying the time-zone avoids dependence on the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
     * Using this method will prevent the ability to use an alternate clock for testing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * @param zone  the zone ID to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * @return the current date using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    public static LocalDate now(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        return now(Clock.system(zone));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * Obtains the current date from the specified clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * This will query the specified clock to obtain the current date - today.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * Using this method allows the use of an alternate clock for testing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * The alternate clock may be introduced using {@link Clock dependency injection}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @param clock  the clock to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * @return the current date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
    public static LocalDate now(Clock clock) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        Objects.requireNonNull(clock, "clock");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
        final Instant now = clock.instant();  // called once
33831
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   224
        return ofInstant(now, clock.getZone());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * Obtains an instance of {@code LocalDate} from a year, month and day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   231
     * This returns a {@code LocalDate} with the specified year, month and day-of-month.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * @param month  the month-of-year to represent, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
     * @return the local date, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   238
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   239
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
    public static LocalDate of(int year, Month month, int dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
        YEAR.checkValidValue(year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
        Objects.requireNonNull(month, "month");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
        DAY_OF_MONTH.checkValidValue(dayOfMonth);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   245
        return create(year, month.getValue(), dayOfMonth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     * Obtains an instance of {@code LocalDate} from a year, month and day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   251
     * This returns a {@code LocalDate} with the specified year, month and day-of-month.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
     * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * @return the local date, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   259
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
    public static LocalDate of(int year, int month, int dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
        YEAR.checkValidValue(year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
        MONTH_OF_YEAR.checkValidValue(month);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
        DAY_OF_MONTH.checkValidValue(dayOfMonth);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   265
        return create(year, month, dayOfMonth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * Obtains an instance of {@code LocalDate} from a year and day-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   272
     * This returns a {@code LocalDate} with the specified year and day-of-year.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * The day-of-year must be valid for the year, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
     * @param dayOfYear  the day-of-year to represent, from 1 to 366
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     * @return the local date, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   278
     * @throws DateTimeException if the value of any field is out of range,
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   279
     *  or if the day-of-year is invalid for the year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
    public static LocalDate ofYearDay(int year, int dayOfYear) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
        YEAR.checkValidValue(year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
        DAY_OF_YEAR.checkValidValue(dayOfYear);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   284
        boolean leap = IsoChronology.INSTANCE.isLeapYear(year);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
        if (dayOfYear == 366 && leap == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
            throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
        Month moy = Month.of((dayOfYear - 1) / 31 + 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
        int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
        if (dayOfYear > monthEnd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
            moy = moy.plus(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
        int dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   294
        return new LocalDate(year, moy.getValue(), dom);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
    /**
33831
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   299
     * Obtains an instance of {@code LocalDate} from an {@code Instant} and zone ID.
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   300
     * <p>
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   301
     * This creates a local date based on the specified instant.
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   302
     * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   303
     * which is simple as there is only one valid offset for each instant.
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   304
     * Then, the instant and offset are used to calculate the local date.
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   305
     *
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   306
     * @param instant  the instant to create the date from, not null
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   307
     * @param zone  the time-zone, which may be an offset, not null
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   308
     * @return the local date, not null
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   309
     * @throws DateTimeException if the result exceeds the supported range
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   310
     */
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   311
    public static LocalDate ofInstant(Instant instant, ZoneId zone) {
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   312
        Objects.requireNonNull(instant, "instant");
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   313
        Objects.requireNonNull(zone, "zone");
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   314
        ZoneRules rules = zone.getRules();
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   315
        ZoneOffset offset = rules.getOffset(instant);
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   316
        long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   317
        long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   318
        return ofEpochDay(localEpochDay);
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   319
    }
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   320
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   321
    //-----------------------------------------------------------------------
022f746e1d74 8133079: java.time LocalDate and LocalTime ofInstant() factory methods
rriggs
parents: 25859
diff changeset
   322
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * Obtains an instance of {@code LocalDate} from the epoch day count.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   325
     * This returns a {@code LocalDate} with the specified epoch-day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   326
     * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   327
     * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
     * @return the local date, not null
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   331
     * @throws DateTimeException if the epoch day exceeds the supported date range
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
    public static LocalDate ofEpochDay(long epochDay) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
        long zeroDay = epochDay + DAYS_0000_TO_1970;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
        // find the march-based year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
        zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
        long adjust = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
        if (zeroDay < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
            // adjust negative years to positive for calculation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
            long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
            adjust = adjustCycles * 400;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
            zeroDay += -adjustCycles * DAYS_PER_CYCLE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
        long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
        long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
        if (doyEst < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
            // fix estimate
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
            yearEst--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
            doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
        yearEst += adjust;  // reset any negative year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
        int marchDoy0 = (int) doyEst;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
        // convert march-based values back to january-based
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
        int marchMonth0 = (marchDoy0 * 5 + 2) / 153;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
        int month = (marchMonth0 + 2) % 12 + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
        int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
        yearEst += marchMonth0 / 10;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
        // check year now we are certain it is correct
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
        int year = YEAR.checkValidIntValue(yearEst);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
        return new LocalDate(year, month, dom);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     * Obtains an instance of {@code LocalDate} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   369
     * This obtains a local date based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   370
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   371
     * which this factory converts to an instance of {@code LocalDate}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
     * <p>
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
   373
     * The conversion uses the {@link TemporalQueries#localDate()} query, which relies
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   374
     * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
     * allowing it to be used as a query via method reference, {@code LocalDate::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
     * @return the local date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * @throws DateTimeException if unable to convert to a {@code LocalDate}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
    public static LocalDate from(TemporalAccessor temporal) {
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
   384
        Objects.requireNonNull(temporal, "temporal");
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
   385
        LocalDate date = temporal.query(TemporalQueries.localDate());
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   386
        if (date == null) {
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   387
            throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " +
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   388
                    temporal + " of type " + temporal.getClass().getName());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   390
        return date;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     * The string must represent a valid date and is parsed using
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   398
     * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     * @param text  the text to parse such as "2007-12-03", not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * @return the parsed local date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
    public static LocalDate parse(CharSequence text) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   405
        return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * The text is parsed using the formatter, returning a date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * @param formatter  the formatter to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * @return the parsed local date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
    public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
        return formatter.parse(text, LocalDate::from);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     * Creates a local date from the year, month and day fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   428
     * @param month  the month-of-year to represent, from 1 to 12, validated
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
     * @param dayOfMonth  the day-of-month to represent, validated from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     * @return the local date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     * @throws DateTimeException if the day-of-month is invalid for the month-year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   433
    private static LocalDate create(int year, int month, int dayOfMonth) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   434
        if (dayOfMonth > 28) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   435
            int dom = 31;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   436
            switch (month) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   437
                case 2:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   438
                    dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   439
                    break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   440
                case 4:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   441
                case 6:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   442
                case 9:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   443
                case 11:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   444
                    dom = 30;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   445
                    break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   446
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   447
            if (dayOfMonth > dom) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   448
                if (dayOfMonth == 29) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   449
                    throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   450
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   451
                    throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   452
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   455
        return new LocalDate(year, month, dayOfMonth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     * Resolves the date, resolving days past the end of month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
     * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
     * @param month  the month-of-year to represent, validated from 1 to 12
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     * @param day  the day-of-month to represent, validated from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
     * @return the resolved date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
    private static LocalDate resolvePreviousValid(int year, int month, int day) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
        switch (month) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
            case 2:
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   469
                day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
            case 4:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
            case 6:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
            case 9:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
            case 11:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
                day = Math.min(day, 30);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   478
        return new LocalDate(year, month, day);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
     * Constructor, previously validated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     * @param month  the month-of-year to represent, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * @param dayOfMonth  the day-of-month to represent, valid for year-month, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
    private LocalDate(int year, int month, int dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
        this.year = year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
        this.month = (short) month;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
        this.day = (short) dayOfMonth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
     * This checks if this date can be queried for the specified field.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   499
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   500
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   501
     * methods will throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * The supported fields are:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     * <li>{@code DAY_OF_WEEK}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     * <li>{@code DAY_OF_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
     * <li>{@code DAY_OF_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
     * <li>{@code EPOCH_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
     * <li>{@code ALIGNED_WEEK_OF_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
     * <li>{@code ALIGNED_WEEK_OF_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
     * <li>{@code MONTH_OF_YEAR}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   515
     * <li>{@code PROLEPTIC_MONTH}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
     * <li>{@code YEAR_OF_ERA}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
     * <li>{@code YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
     * <li>{@code ERA}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   523
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
     * @return true if the field is supported on this date, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
        return ChronoLocalDate.super.isSupported(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   536
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   537
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   538
     * This checks if the specified unit can be added to, or subtracted from, this date.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   539
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   540
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   541
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   542
     * 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
   543
     * The supported units are:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   544
     * <ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   545
     * <li>{@code DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   546
     * <li>{@code WEEKS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   547
     * <li>{@code MONTHS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   548
     * <li>{@code YEARS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   549
     * <li>{@code DECADES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   550
     * <li>{@code CENTURIES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   551
     * <li>{@code MILLENNIA}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   552
     * <li>{@code ERAS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   553
     * </ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   554
     * All other {@code ChronoUnit} instances will return false.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   555
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   556
     * 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
   557
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   558
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   559
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   560
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   561
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   562
     * @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
   563
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   564
    @Override  // override for Javadoc
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   565
    public boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   566
        return ChronoLocalDate.super.isSupported(unit);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   567
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   568
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   569
    //-----------------------------------------------------------------------
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   570
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   574
     * This date is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
     * If it is not possible to return the range, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   578
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
     * The {@link #isSupported(TemporalField) supported fields} will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
     * appropriate range instances.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   581
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   584
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
     * @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
   591
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
            ChronoField f = (ChronoField) field;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   597
            if (f.isDateBased()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
                switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
                    case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
                    case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
                    case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
                    case YEAR_OF_ERA:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
                        return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
                return field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   607
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   608
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   609
        return field.rangeRefinedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
     * Gets the value of the specified field from this date as an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   614
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   615
     * This queries this date for the value of the specified field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
     * The returned value will always be within the valid range of values for the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
     * The {@link #isSupported(TemporalField) supported fields} will return valid
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   622
     * values based on this date, except {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH}
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   623
     * which are too large to fit in an {@code int} and throw an {@code UnsupportedTemporalTypeException}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   624
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   627
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
     * @return the value for the field
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   633
     * @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
   634
     *         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
   635
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   636
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
            return get0(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
        return ChronoLocalDate.super.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
     * Gets the value of the specified field from this date as a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   650
     * This queries this date for the value of the specified field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
     * values based on this date.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   657
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   660
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * @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
   667
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
            if (field == EPOCH_DAY) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
                return toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   676
            if (field == PROLEPTIC_MONTH) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   677
                return getProlepticMonth();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
            return get0(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   681
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
    private int get0(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
        switch ((ChronoField) field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
            case DAY_OF_WEEK: return getDayOfWeek().getValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
            case DAY_OF_MONTH: return day;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
            case DAY_OF_YEAR: return getDayOfYear();
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   691
            case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
            case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
            case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
            case MONTH_OF_YEAR: return month;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   695
            case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
            case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
            case YEAR: return year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
            case ERA: return (year >= 1 ? 1 : 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
        }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   700
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   703
    private long getProlepticMonth() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   704
        return (year * 12L + month - 1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
     * Gets the chronology of this date, which is the ISO calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   711
     * The {@code Chronology} represents the calendar system in use.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
     * The ISO-8601 calendar system is the modern civil calendar system used today
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
     * in most of the world. It is equivalent to the proleptic Gregorian calendar
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   714
     * system, in which today's rules for leap years are applied for all time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
     * @return the ISO chronology, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   719
    public IsoChronology getChronology() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   720
        return IsoChronology.INSTANCE;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   724
     * Gets the era applicable at this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   725
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   726
     * The official ISO-8601 standard does not define eras, however {@code IsoChronology} does.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   727
     * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   728
     * Since dates before the Julian-Gregorian cutover are not in line with history,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   729
     * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
     * eras, often referred to using 'BC' and 'AD'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   731
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   732
     * Users of this class should typically ignore this method as it exists primarily
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   733
     * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   734
     * the Japanese calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
     * The returned era will be a singleton capable of being compared with the constants
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   737
     * in {@link IsoChronology} using the {@code ==} operator.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   738
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   739
     * @return the {@code IsoChronology} era constant applicable at this date, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   740
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   741
    @Override // override for Javadoc
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   742
    public Era getEra() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
        return ChronoLocalDate.super.getEra();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
     * Gets the year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
     * This method returns the primitive {@code int} value for the year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
     * The year returned by this method is proleptic as per {@code get(YEAR)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   752
     * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   753
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
     * @return the year, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
    public int getYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
        return year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
     * Gets the month-of-year field from 1 to 12.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
     * This method returns the month as an {@code int} from 1 to 12.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
     * Application code is frequently clearer if the enum {@link Month}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
     * is used by calling {@link #getMonth()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
     * @return the month-of-year, from 1 to 12
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
     * @see #getMonth()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   769
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   770
    public int getMonthValue() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
        return month;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
     * Gets the month-of-year field using the {@code Month} enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * This method returns the enum {@link Month} for the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
     * This avoids confusion as to what {@code int} values mean.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * If you need access to the primitive {@code int} value then the enum
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
     * provides the {@link Month#getValue() int value}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
     * @return the month-of-year, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
     * @see #getMonthValue()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
    public Month getMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
        return Month.of(month);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
     * Gets the day-of-month field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
     * This method returns the primitive {@code int} value for the day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
     * @return the day-of-month, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
    public int getDayOfMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   797
        return day;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   798
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   800
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
     * Gets the day-of-year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   803
     * This method returns the primitive {@code int} value for the day-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   804
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   805
     * @return the day-of-year, from 1 to 365, or 366 in a leap year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   806
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   807
    public int getDayOfYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   808
        return getMonth().firstDayOfYear(isLeapYear()) + day - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   809
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   810
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
     * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
     * This method returns the enum {@link DayOfWeek} for the day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
     * This avoids confusion as to what {@code int} values mean.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
     * If you need access to the primitive {@code int} value then the enum
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
     * provides the {@link DayOfWeek#getValue() int value}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   819
     * Additional information can be obtained from the {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
     * This includes textual names of the values.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
     * @return the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   823
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   824
    public DayOfWeek getDayOfWeek() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   825
        int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   826
        return DayOfWeek.of(dow0 + 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   827
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   828
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   829
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   830
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
     * Checks if the year is a leap year, according to the ISO proleptic
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   832
     * calendar system rules.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
     * This method applies the current rules for leap years across the whole time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   835
     * In general, a year is a leap year if it is divisible by four without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   836
     * remainder. However, years divisible by 100, are not leap years, with
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
     * the exception of years divisible by 400 which are.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
     * For example, 1904 is a leap year it is divisible by 4.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
     * 1900 was not a leap year as it is divisible by 100, however 2000 was a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   841
     * leap year as it is divisible by 400.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   842
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
     * The calculation is proleptic - applying the same rules into the far future and far past.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
     * This is historically inaccurate, but is correct for the ISO-8601 standard.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
     * @return true if the year is leap, false otherwise
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
    @Override // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
    public boolean isLeapYear() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   850
        return IsoChronology.INSTANCE.isLeapYear(year);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
     * Returns the length of the month represented by this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
     * This returns the length of the month in days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
     * For example, a date in January would return 31.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
     * @return the length of the month in days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
    public int lengthOfMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
        switch (month) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
            case 2:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
                return (isLeapYear() ? 29 : 28);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
            case 4:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
            case 6:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
            case 9:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
            case 11:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
                return 30;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
            default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
                return 31;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   876
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
     * Returns the length of the year represented by this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
     * This returns the length of the year in days, either 365 or 366.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
     * @return 366 if the year is leap, 365 otherwise
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
    @Override // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
    public int lengthOfYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
        return (isLeapYear() ? 366 : 365);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
     * Returns an adjusted copy of this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   892
     * This returns a {@code LocalDate}, based on this one, with the date adjusted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
     * The adjustment takes place using the specified adjuster strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
     * Read the documentation of the adjuster to understand what adjustment will be made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
     * A simple adjuster might simply set the one of the fields, such as the year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     * A more complex adjuster might set the date to the last day of the month.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   898
     * <p>
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   899
     * A selection of common adjustments is provided in
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   900
     * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
     * These include finding the "last day of the month" and "next Wednesday".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
     * Key date-time classes also implement the {@code TemporalAdjuster} interface,
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   903
     * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     * The adjuster is responsible for handling special cases, such as the varying
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     * lengths of month and leap years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
     * For example this code returns a date on the last day of July:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
     *  import static java.time.Month.*;
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
   910
     *  import static java.time.temporal.TemporalAdjusters.*;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
     *  result = localDate.with(JULY).with(lastDayOfMonth());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
     * specified adjuster passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
     * @param adjuster the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
     * @return a {@code LocalDate} based on {@code this} with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
     * @throws DateTimeException if the adjustment cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
    public LocalDate with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
        // optimizations
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
        if (adjuster instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
            return (LocalDate) adjuster;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   931
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   932
        return (LocalDate) adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   933
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   934
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   935
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   936
     * Returns a copy of this date with the specified field set to a new value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   937
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   938
     * This returns a {@code LocalDate}, based on this one, with the value
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   939
     * for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   940
     * This can be used to change any supported field, such as the year, month or day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   941
     * If it is not possible to set the value, because the field is not supported or for
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   942
     * some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   943
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   944
     * In some cases, changing the specified field can cause the resulting date to become invalid,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   945
     * such as changing the month from 31st January to February would make the day-of-month invalid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
     * In cases like this, the field is responsible for resolving the date. Typically it will choose
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
     * the previous valid date, which would be the last valid day of February in this example.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   948
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   949
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   950
     * The supported fields behave as follows:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   951
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   952
     * <li>{@code DAY_OF_WEEK} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
     *  Returns a {@code LocalDate} with the specified day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   954
     *  The date is adjusted up to 6 days forward or backward within the boundary
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   955
     *  of a Monday to Sunday week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   956
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   957
     *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
     *  The date is adjusted to the specified month-based aligned-day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
     *  Aligned weeks are counted such that the first week of a given month starts
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   960
     *  on the first day of that month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   961
     *  This may cause the date to be moved up to 6 days into the following month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   962
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   963
     *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   964
     *  The date is adjusted to the specified year-based aligned-day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   965
     *  Aligned weeks are counted such that the first week of a given year starts
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   966
     *  on the first day of that year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   967
     *  This may cause the date to be moved up to 6 days into the following year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
     * <li>{@code DAY_OF_MONTH} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   969
     *  Returns a {@code LocalDate} with the specified day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   970
     *  The month and year will be unchanged. If the day-of-month is invalid for the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   971
     *  year and month, then a {@code DateTimeException} is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   972
     * <li>{@code DAY_OF_YEAR} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     *  Returns a {@code LocalDate} with the specified day-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
     *  The year will be unchanged. If the day-of-year is invalid for the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
     *  year, then a {@code DateTimeException} is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
     * <li>{@code EPOCH_DAY} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
     *  Returns a {@code LocalDate} with the specified epoch-day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
     *  This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
     * <li>{@code ALIGNED_WEEK_OF_MONTH} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
     *  Returns a {@code LocalDate} with the specified aligned-week-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
     *  Aligned weeks are counted such that the first week of a given month starts
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
     *  on the first day of that month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
     *  This adjustment moves the date in whole week chunks to match the specified week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
     *  The result will have the same day-of-week as this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
     *  This may cause the date to be moved into the following month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
     * <li>{@code ALIGNED_WEEK_OF_YEAR} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   987
     *  Returns a {@code LocalDate} with the specified aligned-week-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     *  Aligned weeks are counted such that the first week of a given year starts
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
     *  on the first day of that year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
     *  This adjustment moves the date in whole week chunks to match the specified week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
     *  The result will have the same day-of-week as this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
     *  This may cause the date to be moved into the following year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
     * <li>{@code MONTH_OF_YEAR} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
     *  Returns a {@code LocalDate} with the specified month-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
     *  The year will be unchanged. The day-of-month will also be unchanged,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
     *  unless it would be invalid for the new month and year. In that case, the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   998
     * <li>{@code PROLEPTIC_MONTH} -
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   999
     *  Returns a {@code LocalDate} with the specified proleptic-month.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
     *  The day-of-month will be unchanged, unless it would be invalid for the new month
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
     *  and year. In that case, the day-of-month is adjusted to the maximum valid value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
     *  for the new month and year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     * <li>{@code YEAR_OF_ERA} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
     *  Returns a {@code LocalDate} with the specified year-of-era.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     *  The era and month will be unchanged. The day-of-month will also be unchanged,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
     *  unless it would be invalid for the new month and year. In that case, the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
     * <li>{@code YEAR} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
     *  Returns a {@code LocalDate} with the specified year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
     *  The month will be unchanged. The day-of-month will also be unchanged,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
     *  unless it would be invalid for the new month and year. In that case, the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
     * <li>{@code ERA} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
     *  Returns a {@code LocalDate} with the specified era.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
     *  The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
     *  unless it would be invalid for the new month and year. In that case, the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1018
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * In all cases, if the new value is outside the valid range of values for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1021
     * then a {@code DateTimeException} will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1023
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1026
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
     * passing {@code this} as the argument. In this case, the field determines
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
     * whether and how to adjust the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
     * @return a {@code LocalDate} based on {@code this} with the specified field set, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1036
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
    public LocalDate with(TemporalField field, long newValue) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1042
            ChronoField f = (ChronoField) field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
            f.checkValidValue(newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
                case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
                case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
                case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
                case DAY_OF_MONTH: return withDayOfMonth((int) newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
                case DAY_OF_YEAR: return withDayOfYear((int) newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
                case EPOCH_DAY: return LocalDate.ofEpochDay(newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
                case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
                case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1053
                case MONTH_OF_YEAR: return withMonth((int) newValue);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1054
                case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
                case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1056
                case YEAR: return withYear((int) newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1057
                case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1058
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1059
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1061
        return field.adjustInto(this, newValue);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1066
     * Returns a copy of this {@code LocalDate} with the year altered.
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1067
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
     * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
     * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
     * @return a {@code LocalDate} based on this date with the requested year, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
     * @throws DateTimeException if the year value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
    public LocalDate withYear(int year) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
        if (this.year == year) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
        YEAR.checkValidValue(year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
        return resolvePreviousValid(year, month, day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1085
     * Returns a copy of this {@code LocalDate} with the month-of-year altered.
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1086
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
     * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1090
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
     * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
     * @return a {@code LocalDate} based on this date with the requested month, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
     * @throws DateTimeException if the month-of-year value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1094
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1095
    public LocalDate withMonth(int month) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1096
        if (this.month == month) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1097
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
        MONTH_OF_YEAR.checkValidValue(month);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
        return resolvePreviousValid(year, month, day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1104
     * Returns a copy of this {@code LocalDate} with the day-of-month altered.
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1105
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
     * If the resulting date is invalid, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1107
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1108
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1109
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1110
     * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
     * @return a {@code LocalDate} based on this date with the requested day, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1112
     * @throws DateTimeException if the day-of-month value is invalid,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1113
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1114
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1115
    public LocalDate withDayOfMonth(int dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1116
        if (this.day == dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1117
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1118
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
        return of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1120
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1121
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1122
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1123
     * Returns a copy of this {@code LocalDate} with the day-of-year altered.
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1124
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
     * If the resulting date is invalid, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1126
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
     * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
     * @return a {@code LocalDate} based on this date with the requested day, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1131
     * @throws DateTimeException if the day-of-year value is invalid,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1132
     *  or if the day-of-year is invalid for the year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1134
    public LocalDate withDayOfYear(int dayOfYear) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1135
        if (this.getDayOfYear() == dayOfYear) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1136
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1137
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1138
        return ofYearDay(year, dayOfYear);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1139
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1142
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1143
     * Returns a copy of this date with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1144
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1145
     * This returns a {@code LocalDate}, based on this one, with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1146
     * The amount is typically {@link Period} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1147
     * the {@link TemporalAmount} interface.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1148
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1149
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1150
     * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1151
     * to implement the addition in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1152
     * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1153
     * of the amount implementation to determine if it can be successfully added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1154
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1155
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1156
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1157
     * @param amountToAdd  the amount to add, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
     * @return a {@code LocalDate} based on this date with the addition made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1159
     * @throws DateTimeException if the addition cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1160
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1161
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1162
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1163
    public LocalDate plus(TemporalAmount amountToAdd) {
20517
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1164
        if (amountToAdd instanceof Period) {
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1165
            Period periodToAdd = (Period) amountToAdd;
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1166
            return plusMonths(periodToAdd.toTotalMonths()).plusDays(periodToAdd.getDays());
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1167
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1168
        Objects.requireNonNull(amountToAdd, "amountToAdd");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1169
        return (LocalDate) amountToAdd.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1170
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1171
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1172
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1173
     * Returns a copy of this date with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1174
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1175
     * This returns a {@code LocalDate}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1176
     * 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: 15289
diff changeset
  1177
     * unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1178
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1179
     * In some cases, adding the amount can cause the resulting date to become invalid.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1180
     * For example, adding one month to 31st January would result in 31st February.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1181
     * In cases like this, the unit is responsible for resolving the date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1182
     * Typically it will choose the previous valid date, which would be the last valid
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1183
     * day of February in this example.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1184
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1185
     * If the field is a {@link ChronoUnit} then the addition is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1186
     * The supported fields behave as follows:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1187
     * <ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1188
     * <li>{@code DAYS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1189
     *  Returns a {@code LocalDate} with the specified number of days added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1190
     *  This is equivalent to {@link #plusDays(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1191
     * <li>{@code WEEKS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1192
     *  Returns a {@code LocalDate} with the specified number of weeks added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1193
     *  This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1194
     * <li>{@code MONTHS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1195
     *  Returns a {@code LocalDate} with the specified number of months added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1196
     *  This is equivalent to {@link #plusMonths(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1197
     *  The day-of-month will be unchanged unless it would be invalid for the new
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1198
     *  month and year. In that case, the day-of-month is adjusted to the maximum
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1199
     *  valid value for the new month and year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1200
     * <li>{@code YEARS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1201
     *  Returns a {@code LocalDate} with the specified number of years added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1202
     *  This is equivalent to {@link #plusYears(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1203
     *  The day-of-month will be unchanged unless it would be invalid for the new
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1204
     *  month and year. In that case, the day-of-month is adjusted to the maximum
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1205
     *  valid value for the new month and year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1206
     * <li>{@code DECADES} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1207
     *  Returns a {@code LocalDate} with the specified number of decades added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1208
     *  This is equivalent to calling {@link #plusYears(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1209
     *  multiplied by 10.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1210
     *  The day-of-month will be unchanged unless it would be invalid for the new
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1211
     *  month and year. In that case, the day-of-month is adjusted to the maximum
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1212
     *  valid value for the new month and year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1213
     * <li>{@code CENTURIES} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1214
     *  Returns a {@code LocalDate} with the specified number of centuries added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1215
     *  This is equivalent to calling {@link #plusYears(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1216
     *  multiplied by 100.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1217
     *  The day-of-month will be unchanged unless it would be invalid for the new
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1218
     *  month and year. In that case, the day-of-month is adjusted to the maximum
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1219
     *  valid value for the new month and year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1220
     * <li>{@code MILLENNIA} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1221
     *  Returns a {@code LocalDate} with the specified number of millennia added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1222
     *  This is equivalent to calling {@link #plusYears(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1223
     *  multiplied by 1,000.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1224
     *  The day-of-month will be unchanged unless it would be invalid for the new
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1225
     *  month and year. In that case, the day-of-month is adjusted to the maximum
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1226
     *  valid value for the new month and year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1227
     * <li>{@code ERAS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1228
     *  Returns a {@code LocalDate} with the specified number of eras added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1229
     *  Only two eras are supported so the amount must be one, zero or minus one.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1230
     *  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: 15289
diff changeset
  1231
     *  is unchanged.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1232
     *  The day-of-month will be unchanged unless it would be invalid for the new
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1233
     *  month and year. In that case, the day-of-month is adjusted to the maximum
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1234
     *  valid value for the new month and year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1235
     * </ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1236
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1237
     * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1238
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1239
     * If the field is not a {@code ChronoUnit}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1240
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1241
     * passing {@code this} as the argument. In this case, the unit determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1242
     * whether and how to perform the addition.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1245
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1246
     * @param amountToAdd  the amount of the unit to add to the result, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1247
     * @param unit  the unit of the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1248
     * @return a {@code LocalDate} based on this date with the specified amount added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1249
     * @throws DateTimeException if the addition cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1250
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1251
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1252
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1253
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1254
    public LocalDate plus(long amountToAdd, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1255
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1256
            ChronoUnit f = (ChronoUnit) unit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
                case DAYS: return plusDays(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1259
                case WEEKS: return plusWeeks(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1260
                case MONTHS: return plusMonths(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1261
                case YEARS: return plusYears(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1262
                case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1263
                case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1264
                case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1265
                case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1266
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1267
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1268
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1269
        return unit.addTo(this, amountToAdd);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1270
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1271
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1272
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1273
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1274
     * Returns a copy of this {@code LocalDate} with the specified number of years added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1275
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1276
     * This method adds the specified amount to the years field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1277
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1278
     * <li>Add the input years to the year field</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1279
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1280
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1281
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1282
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1283
     * For example, 2008-02-29 (leap year) plus one year would result in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1284
     * invalid date 2009-02-29 (standard year). Instead of returning an invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1285
     * result, the last valid day of the month, 2009-02-28, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1286
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1287
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1288
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1289
     * @param yearsToAdd  the years to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1290
     * @return a {@code LocalDate} based on this date with the years added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1291
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1292
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1293
    public LocalDate plusYears(long yearsToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1294
        if (yearsToAdd == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1295
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1296
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1297
        int newYear = YEAR.checkValidIntValue(year + yearsToAdd);  // safe overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1298
        return resolvePreviousValid(newYear, month, day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1299
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1300
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1301
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1302
     * Returns a copy of this {@code LocalDate} with the specified number of months added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1303
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1304
     * This method adds the specified amount to the months field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1305
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1306
     * <li>Add the input months to the month-of-year field</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1307
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1308
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1309
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1310
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1311
     * For example, 2007-03-31 plus one month would result in the invalid date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1312
     * 2007-04-31. Instead of returning an invalid result, the last valid day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1313
     * of the month, 2007-04-30, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1314
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1315
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1316
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1317
     * @param monthsToAdd  the months to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1318
     * @return a {@code LocalDate} based on this date with the months added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1319
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1320
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1321
    public LocalDate plusMonths(long monthsToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1322
        if (monthsToAdd == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1323
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1324
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1325
        long monthCount = year * 12L + (month - 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1326
        long calcMonths = monthCount + monthsToAdd;  // safe overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1327
        int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1328
        int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1329
        return resolvePreviousValid(newYear, newMonth, day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1330
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1331
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1332
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1333
     * Returns a copy of this {@code LocalDate} with the specified number of weeks added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1334
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1335
     * This method adds the specified amount in weeks to the days field incrementing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1336
     * the month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1337
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1338
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1339
     * For example, 2008-12-31 plus one week would result in 2009-01-07.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1340
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1341
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1342
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1343
     * @param weeksToAdd  the weeks to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1344
     * @return a {@code LocalDate} based on this date with the weeks added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1345
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1346
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1347
    public LocalDate plusWeeks(long weeksToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1348
        return plusDays(Math.multiplyExact(weeksToAdd, 7));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1349
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1350
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1351
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1352
     * Returns a copy of this {@code LocalDate} with the specified number of days added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1353
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1354
     * This method adds the specified amount to the days field incrementing the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1355
     * month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1356
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1357
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1358
     * For example, 2008-12-31 plus one day would result in 2009-01-01.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1359
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1360
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1361
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1362
     * @param daysToAdd  the days to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1363
     * @return a {@code LocalDate} based on this date with the days added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1364
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1365
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1366
    public LocalDate plusDays(long daysToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1367
        if (daysToAdd == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1368
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1369
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1370
        long mjDay = Math.addExact(toEpochDay(), daysToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1371
        return LocalDate.ofEpochDay(mjDay);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1372
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1373
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1374
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1375
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1376
     * Returns a copy of this date with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1377
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1378
     * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1379
     * The amount is typically {@link Period} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1380
     * the {@link TemporalAmount} interface.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1381
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1382
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1383
     * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1384
     * to implement the subtraction in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1385
     * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1386
     * of the amount implementation to determine if it can be successfully subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1387
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1388
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1389
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1390
     * @param amountToSubtract  the amount to subtract, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1391
     * @return a {@code LocalDate} based on this date with the subtraction made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1392
     * @throws DateTimeException if the subtraction cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1393
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1394
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1395
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1396
    public LocalDate minus(TemporalAmount amountToSubtract) {
20517
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1397
        if (amountToSubtract instanceof Period) {
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1398
            Period periodToSubtract = (Period) amountToSubtract;
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1399
            return minusMonths(periodToSubtract.toTotalMonths()).minusDays(periodToSubtract.getDays());
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1400
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1401
        Objects.requireNonNull(amountToSubtract, "amountToSubtract");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1402
        return (LocalDate) amountToSubtract.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1403
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1404
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1405
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1406
     * Returns a copy of this date with the specified amount subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1407
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1408
     * This returns a {@code LocalDate}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1409
     * in terms of the unit subtracted. If it is not possible to subtract the amount,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1410
     * because the unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1411
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1412
     * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1413
     * See that method for a full description of how addition, and thus subtraction, works.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1414
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1415
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1416
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1417
     * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1418
     * @param unit  the unit of the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1419
     * @return a {@code LocalDate} based on this date with the specified amount subtracted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1420
     * @throws DateTimeException if the subtraction cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1421
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1422
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1423
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1424
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1425
    public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1426
        return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1427
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1428
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1429
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1430
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1431
     * Returns a copy of this {@code LocalDate} with the specified number of years subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1432
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1433
     * This method subtracts the specified amount from the years field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1434
     * <ol>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1435
     * <li>Subtract the input years from the year field</li>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1436
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1437
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1438
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1439
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1440
     * For example, 2008-02-29 (leap year) minus one year would result in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1441
     * invalid date 2007-02-29 (standard year). Instead of returning an invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1442
     * result, the last valid day of the month, 2007-02-28, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1443
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1444
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1445
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1446
     * @param yearsToSubtract  the years to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1447
     * @return a {@code LocalDate} based on this date with the years subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1448
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1449
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1450
    public LocalDate minusYears(long yearsToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1451
        return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1452
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1453
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1454
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1455
     * Returns a copy of this {@code LocalDate} with the specified number of months subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1456
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1457
     * This method subtracts the specified amount from the months field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1458
     * <ol>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1459
     * <li>Subtract the input months from the month-of-year field</li>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1460
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1461
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1462
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1463
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1464
     * For example, 2007-03-31 minus one month would result in the invalid date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1465
     * 2007-02-31. Instead of returning an invalid result, the last valid day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1466
     * of the month, 2007-02-28, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1467
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1468
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1469
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1470
     * @param monthsToSubtract  the months to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1471
     * @return a {@code LocalDate} based on this date with the months subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1472
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1473
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1474
    public LocalDate minusMonths(long monthsToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1475
        return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1476
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1477
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1478
    /**
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22566
diff changeset
  1479
     * Returns a copy of this {@code LocalDate} with the specified number of weeks subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1480
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1481
     * This method subtracts the specified amount in weeks from the days field decrementing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1482
     * the month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1483
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1484
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1485
     * For example, 2009-01-07 minus one week would result in 2008-12-31.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1486
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1487
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1488
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1489
     * @param weeksToSubtract  the weeks to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1490
     * @return a {@code LocalDate} based on this date with the weeks subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1491
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1492
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1493
    public LocalDate minusWeeks(long weeksToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1494
        return (weeksToSubtract == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeksToSubtract));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1495
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1496
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1497
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1498
     * Returns a copy of this {@code LocalDate} with the specified number of days subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1499
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1500
     * This method subtracts the specified amount from the days field decrementing the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1501
     * month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1502
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1503
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1504
     * For example, 2009-01-01 minus one day would result in 2008-12-31.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1505
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1506
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1507
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1508
     * @param daysToSubtract  the days to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1509
     * @return a {@code LocalDate} based on this date with the days subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1510
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1511
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1512
    public LocalDate minusDays(long daysToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1513
        return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1514
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1515
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1516
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1517
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1518
     * Queries this date using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1519
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1520
     * This queries this date using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1521
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1522
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1523
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1524
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1525
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1526
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1527
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1528
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1529
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1530
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1531
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1532
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1533
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1534
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1535
    @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1536
    @Override
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1537
    public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1538
        if (query == TemporalQueries.localDate()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1539
            return (R) this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1540
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1541
        return ChronoLocalDate.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1542
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1543
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1544
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1545
     * Adjusts the specified temporal object to have the same date as this object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1546
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1547
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1548
     * with the date changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1549
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1550
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1551
     * passing {@link ChronoField#EPOCH_DAY} as the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1552
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1553
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1554
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1555
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1556
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1557
     *   temporal = thisLocalDate.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1558
     *   temporal = temporal.with(thisLocalDate);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1559
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1560
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1561
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1562
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1563
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1564
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1565
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1566
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1567
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1568
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1569
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1570
        return ChronoLocalDate.super.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1571
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1572
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1573
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1574
     * Calculates the amount of time until another date in terms of the specified unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1575
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1576
     * This calculates the amount of time between two {@code LocalDate}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1577
     * objects in terms of a single {@code TemporalUnit}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1578
     * The start and end points are {@code this} and the specified date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1579
     * The result will be negative if the end is before the start.
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1580
     * The {@code Temporal} passed to this method is converted to a
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1581
     * {@code LocalDate} using {@link #from(TemporalAccessor)}.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1582
     * For example, the amount in days between two dates can be calculated
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1583
     * using {@code startDate.until(endDate, DAYS)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1584
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1585
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1586
     * complete units between the two dates.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1587
     * For example, the amount in months between 2012-06-15 and 2012-08-14
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1588
     * will only be one month as it is one day short of two months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1589
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1590
     * There are two equivalent ways of using this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1591
     * The first is to invoke this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1592
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1593
     * <pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1594
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1595
     *   amount = start.until(end, MONTHS);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1596
     *   amount = MONTHS.between(start, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1597
     * </pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1598
     * The choice should be made based on which makes the code more readable.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1599
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1600
     * The calculation is implemented in this method for {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1601
     * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1602
     * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1603
     * are supported. Other {@code ChronoUnit} values will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1604
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1605
     * If the unit is not a {@code ChronoUnit}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1606
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1607
     * passing {@code this} as the first argument and the converted input temporal
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1608
     * as the second argument.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1609
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1610
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1611
     *
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1612
     * @param endExclusive  the end date, exclusive, which is converted to a {@code LocalDate}, not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1613
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1614
     * @return the amount of time between this date and the end date
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1615
     * @throws DateTimeException if the amount cannot be calculated, or the end
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1616
     *  temporal cannot be converted to a {@code LocalDate}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1617
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1618
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1619
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1620
    @Override
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1621
    public long until(Temporal endExclusive, TemporalUnit unit) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1622
        LocalDate end = LocalDate.from(endExclusive);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1623
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1624
            switch ((ChronoUnit) unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1625
                case DAYS: return daysUntil(end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1626
                case WEEKS: return daysUntil(end) / 7;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1627
                case MONTHS: return monthsUntil(end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1628
                case YEARS: return monthsUntil(end) / 12;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1629
                case DECADES: return monthsUntil(end) / 120;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1630
                case CENTURIES: return monthsUntil(end) / 1200;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1631
                case MILLENNIA: return monthsUntil(end) / 12000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1632
                case ERAS: return end.getLong(ERA) - getLong(ERA);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1633
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1634
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1635
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20519
diff changeset
  1636
        return unit.between(this, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1637
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1638
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1639
    long daysUntil(LocalDate end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1640
        return end.toEpochDay() - toEpochDay();  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1641
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1642
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1643
    private long monthsUntil(LocalDate end) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1644
        long packed1 = getProlepticMonth() * 32L + getDayOfMonth();  // no overflow
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1645
        long packed2 = end.getProlepticMonth() * 32L + end.getDayOfMonth();  // no overflow
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1646
        return (packed2 - packed1) / 32;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1647
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1648
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1649
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1650
     * Calculates the period between this date and another date as a {@code Period}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1651
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1652
     * This calculates the period between two dates in terms of years, months and days.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1653
     * The start and end points are {@code this} and the specified date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1654
     * The result will be negative if the end is before the start.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1655
     * The negative sign will be the same in each of year, month and day.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1656
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1657
     * The calculation is performed using the ISO calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1658
     * If necessary, the input date will be converted to ISO.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1659
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1660
     * The start date is included, but the end date is not.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1661
     * The period is calculated by removing complete months, then calculating
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1662
     * the remaining number of days, adjusting to ensure that both have the same sign.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1663
     * The number of months is then normalized into years and months based on a 12 month year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1664
     * A month is considered to be complete if the end day-of-month is greater
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1665
     * than or equal to the start day-of-month.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1666
     * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1667
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1668
     * There are two equivalent ways of using this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1669
     * The first is to invoke this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1670
     * The second is to use {@link Period#between(LocalDate, LocalDate)}:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1671
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1672
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1673
     *   period = start.until(end);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1674
     *   period = Period.between(start, end);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1675
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1676
     * The choice should be made based on which makes the code more readable.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1677
     *
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 20517
diff changeset
  1678
     * @param endDateExclusive  the end date, exclusive, which may be in any chronology, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1679
     * @return the period between this date and the end date, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1680
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1681
    @Override
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 20517
diff changeset
  1682
    public Period until(ChronoLocalDate endDateExclusive) {
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 20517
diff changeset
  1683
        LocalDate end = LocalDate.from(endDateExclusive);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1684
        long totalMonths = end.getProlepticMonth() - this.getProlepticMonth();  // safe
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1685
        int days = end.day - this.day;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1686
        if (totalMonths > 0 && days < 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1687
            totalMonths--;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1688
            LocalDate calcDate = this.plusMonths(totalMonths);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1689
            days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1690
        } else if (totalMonths < 0 && days > 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1691
            totalMonths++;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1692
            days -= end.lengthOfMonth();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1693
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1694
        long years = totalMonths / 12;  // safe
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1695
        int months = (int) (totalMonths % 12);  // safe
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1696
        return Period.of(Math.toIntExact(years), months, days);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1697
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1698
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1699
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1700
     * Formats this date using the specified formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1701
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1702
     * This date will be passed to the formatter to produce a string.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1703
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1704
     * @param formatter  the formatter to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1705
     * @return the formatted date string, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1706
     * @throws DateTimeException if an error occurs during printing
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1707
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1708
    @Override  // override for Javadoc and performance
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1709
    public String format(DateTimeFormatter formatter) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1710
        Objects.requireNonNull(formatter, "formatter");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1711
        return formatter.format(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1712
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1713
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1714
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1715
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1716
     * Combines this date with a time to create a {@code LocalDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1717
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1718
     * This returns a {@code LocalDateTime} formed from this date at the specified time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1719
     * All possible combinations of date and time are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1720
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1721
     * @param time  the time to combine with, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1722
     * @return the local date-time formed from this date and the specified time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1723
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1724
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1725
    public LocalDateTime atTime(LocalTime time) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1726
        return LocalDateTime.of(this, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1727
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1728
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1729
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1730
     * Combines this date with a time to create a {@code LocalDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1731
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1732
     * This returns a {@code LocalDateTime} formed from this date at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1733
     * specified hour and minute.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1734
     * The seconds and nanosecond fields will be set to zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1735
     * The individual time fields must be within their valid range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1736
     * All possible combinations of date and time are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1737
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1738
     * @param hour  the hour-of-day to use, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1739
     * @param minute  the minute-of-hour to use, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1740
     * @return the local date-time formed from this date and the specified time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1741
     * @throws DateTimeException if the value of any field is out of range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1742
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1743
    public LocalDateTime atTime(int hour, int minute) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1744
        return atTime(LocalTime.of(hour, minute));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1745
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1746
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1747
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1748
     * Combines this date with a time to create a {@code LocalDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1749
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1750
     * This returns a {@code LocalDateTime} formed from this date at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1751
     * specified hour, minute and second.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1752
     * The nanosecond field will be set to zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1753
     * The individual time fields must be within their valid range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1754
     * All possible combinations of date and time are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1755
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1756
     * @param hour  the hour-of-day to use, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1757
     * @param minute  the minute-of-hour to use, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1758
     * @param second  the second-of-minute to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1759
     * @return the local date-time formed from this date and the specified time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1760
     * @throws DateTimeException if the value of any field is out of range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1761
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1762
    public LocalDateTime atTime(int hour, int minute, int second) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1763
        return atTime(LocalTime.of(hour, minute, second));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1764
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1765
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1766
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1767
     * Combines this date with a time to create a {@code LocalDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1768
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1769
     * This returns a {@code LocalDateTime} formed from this date at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1770
     * specified hour, minute, second and nanosecond.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1771
     * The individual time fields must be within their valid range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1772
     * All possible combinations of date and time are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1773
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1774
     * @param hour  the hour-of-day to use, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1775
     * @param minute  the minute-of-hour to use, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1776
     * @param second  the second-of-minute to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1777
     * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1778
     * @return the local date-time formed from this date and the specified time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1779
     * @throws DateTimeException if the value of any field is out of range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1780
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1781
    public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1782
        return atTime(LocalTime.of(hour, minute, second, nanoOfSecond));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1783
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1784
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1785
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1786
     * Combines this date with an offset time to create an {@code OffsetDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1787
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1788
     * This returns an {@code OffsetDateTime} formed from this date at the specified time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1789
     * All possible combinations of date and time are valid.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1790
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1791
     * @param time  the time to combine with, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1792
     * @return the offset date-time formed from this date and the specified time, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1793
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1794
    public OffsetDateTime atTime(OffsetTime time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1795
        return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1796
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1797
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1798
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1799
     * Combines this date with the time of midnight to create a {@code LocalDateTime}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1800
     * at the start of this date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1801
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1802
     * This returns a {@code LocalDateTime} formed from this date at the time of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1803
     * midnight, 00:00, at the start of this date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1804
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1805
     * @return the local date-time of midnight at the start of this date, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1806
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1807
    public LocalDateTime atStartOfDay() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1808
        return LocalDateTime.of(this, LocalTime.MIDNIGHT);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1809
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1810
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1811
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1812
     * Returns a zoned date-time from this date at the earliest valid time according
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1813
     * to the rules in the time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1814
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1815
     * Time-zone rules, such as daylight savings, mean that not every local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1816
     * is valid for the specified zone, thus the local date-time may not be midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1817
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1818
     * In most cases, there is only one valid offset for a local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1819
     * In the case of an overlap, there are two valid offsets, and the earlier one is used,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1820
     * corresponding to the first occurrence of midnight on the date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1821
     * In the case of a gap, the zoned date-time will represent the instant just after the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1822
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1823
     * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1824
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1825
     * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1826
     * followed by {@link LocalDateTime#atZone(ZoneId)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1827
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1828
     * @param zone  the zone ID to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1829
     * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1830
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1831
    public ZonedDateTime atStartOfDay(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1832
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1833
        // need to handle case where there is a gap from 11:30 to 00:30
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1834
        // standard ZDT factory would result in 01:00 rather than 00:30
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1835
        LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1836
        if (zone instanceof ZoneOffset == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1837
            ZoneRules rules = zone.getRules();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1838
            ZoneOffsetTransition trans = rules.getTransition(ldt);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1839
            if (trans != null && trans.isGap()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1840
                ldt = trans.getDateTimeAfter();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1841
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1842
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1843
        return ZonedDateTime.of(ldt, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1844
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1845
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1846
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1847
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1848
    public long toEpochDay() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1849
        long y = year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1850
        long m = month;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1851
        long total = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1852
        total += 365 * y;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1853
        if (y >= 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1854
            total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1855
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1856
            total -= y / -4 - y / -100 + y / -400;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1857
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1858
        total += ((367 * m - 362) / 12);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1859
        total += day - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1860
        if (m > 2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1861
            total--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1862
            if (isLeapYear() == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1863
                total--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1864
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1865
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1866
        return total - DAYS_0000_TO_1970;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1867
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1868
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1869
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1870
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1871
     * Compares this date to another date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1872
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1873
     * The comparison is primarily based on the date, from earliest to latest.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1874
     * It is "consistent with equals", as defined by {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1875
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1876
     * If all the dates being compared are instances of {@code LocalDate},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1877
     * then the comparison will be entirely based on the date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1878
     * If some dates being compared are in different chronologies, then the
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1879
     * chronology is also considered, see {@link java.time.chrono.ChronoLocalDate#compareTo}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1880
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1881
     * @param other  the other date to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1882
     * @return the comparator value, negative if less, positive if greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1883
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1884
    @Override  // override for Javadoc and performance
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1885
    public int compareTo(ChronoLocalDate other) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1886
        if (other instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1887
            return compareTo0((LocalDate) other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1888
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1889
        return ChronoLocalDate.super.compareTo(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1890
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1891
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1892
    int compareTo0(LocalDate otherDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1893
        int cmp = (year - otherDate.year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1894
        if (cmp == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1895
            cmp = (month - otherDate.month);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1896
            if (cmp == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1897
                cmp = (day - otherDate.day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1898
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1899
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1900
        return cmp;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1901
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1902
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1903
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1904
     * Checks if this date is after the specified date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1905
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1906
     * This checks to see if this date represents a point on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1907
     * local time-line after the other date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1908
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1909
     *   LocalDate a = LocalDate.of(2012, 6, 30);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1910
     *   LocalDate b = LocalDate.of(2012, 7, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1911
     *   a.isAfter(b) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1912
     *   a.isAfter(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1913
     *   b.isAfter(a) == true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1914
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1915
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1916
     * This method only considers the position of the two dates on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1917
     * It does not take into account the chronology, or calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1918
     * This is different from the comparison in {@link #compareTo(ChronoLocalDate)},
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1919
     * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1920
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1921
     * @param other  the other date to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1922
     * @return true if this date is after the specified date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1923
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1924
    @Override  // override for Javadoc and performance
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1925
    public boolean isAfter(ChronoLocalDate other) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1926
        if (other instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1927
            return compareTo0((LocalDate) other) > 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1928
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1929
        return ChronoLocalDate.super.isAfter(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1930
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1931
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1932
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1933
     * Checks if this date is before the specified date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1934
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1935
     * This checks to see if this date represents a point on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1936
     * local time-line before the other date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1937
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1938
     *   LocalDate a = LocalDate.of(2012, 6, 30);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1939
     *   LocalDate b = LocalDate.of(2012, 7, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1940
     *   a.isBefore(b) == true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1941
     *   a.isBefore(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1942
     *   b.isBefore(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1943
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1944
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1945
     * This method only considers the position of the two dates on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1946
     * It does not take into account the chronology, or calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1947
     * This is different from the comparison in {@link #compareTo(ChronoLocalDate)},
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1948
     * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1949
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1950
     * @param other  the other date to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1951
     * @return true if this date is before the specified date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1952
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1953
    @Override  // override for Javadoc and performance
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1954
    public boolean isBefore(ChronoLocalDate other) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1955
        if (other instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1956
            return compareTo0((LocalDate) other) < 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1957
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1958
        return ChronoLocalDate.super.isBefore(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1959
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1960
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1961
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1962
     * Checks if this date is equal to the specified date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1963
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1964
     * This checks to see if this date represents the same point on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1965
     * local time-line as the other date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1966
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1967
     *   LocalDate a = LocalDate.of(2012, 6, 30);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1968
     *   LocalDate b = LocalDate.of(2012, 7, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1969
     *   a.isEqual(b) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1970
     *   a.isEqual(a) == true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1971
     *   b.isEqual(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1972
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1973
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1974
     * This method only considers the position of the two dates on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1975
     * It does not take into account the chronology, or calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1976
     * This is different from the comparison in {@link #compareTo(ChronoLocalDate)}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1977
     * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1978
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1979
     * @param other  the other date to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1980
     * @return true if this date is equal to the specified date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1981
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1982
    @Override  // override for Javadoc and performance
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1983
    public boolean isEqual(ChronoLocalDate other) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1984
        if (other instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1985
            return compareTo0((LocalDate) other) == 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1986
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1987
        return ChronoLocalDate.super.isEqual(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1988
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1989
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1990
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1991
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1992
     * Checks if this date is equal to another date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1993
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1994
     * Compares this {@code LocalDate} with another ensuring that the date is the same.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1995
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1996
     * Only objects of type {@code LocalDate} are compared, other types return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1997
     * To compare the dates of two {@code TemporalAccessor} instances, including dates
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1998
     * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1999
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2000
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2001
     * @return true if this is equal to the other date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2002
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2003
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2004
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2005
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2006
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2007
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2008
        if (obj instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2009
            return compareTo0((LocalDate) obj) == 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2010
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2011
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2012
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2013
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2014
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2015
     * A hash code for this date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2016
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2017
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2018
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2019
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2020
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2021
        int yearValue = year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2022
        int monthValue = month;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2023
        int dayValue = day;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2024
        return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2025
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2026
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2027
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2028
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2029
     * Outputs this date as a {@code String}, such as {@code 2007-12-03}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2030
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2031
     * The output will be in the ISO-8601 format {@code uuuu-MM-dd}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2032
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2033
     * @return a string representation of this date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2034
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2035
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2036
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2037
        int yearValue = year;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2038
        int monthValue = month;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2039
        int dayValue = day;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2040
        int absYear = Math.abs(yearValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2041
        StringBuilder buf = new StringBuilder(10);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2042
        if (absYear < 1000) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2043
            if (yearValue < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2044
                buf.append(yearValue - 10000).deleteCharAt(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2045
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2046
                buf.append(yearValue + 10000).deleteCharAt(0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2047
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2048
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2049
            if (yearValue > 9999) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2050
                buf.append('+');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2051
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2052
            buf.append(yearValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2053
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2054
        return buf.append(monthValue < 10 ? "-0" : "-")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2055
            .append(monthValue)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2056
            .append(dayValue < 10 ? "-0" : "-")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2057
            .append(dayValue)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2058
            .toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2059
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2060
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2061
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2062
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2063
     * Writes the object using a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2064
     * <a href="../../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
  2065
     * @serialData
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2066
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  2067
     *  out.writeByte(3);  // identifies a LocalDate
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2068
     *  out.writeInt(year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2069
     *  out.writeByte(month);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2070
     *  out.writeByte(day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2071
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2072
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2073
     * @return the instance of {@code Ser}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2074
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2075
    private Object writeReplace() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2076
        return new Ser(Ser.LOCAL_DATE_TYPE, this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2077
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2078
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2079
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2080
     * Defend against malicious streams.
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20795
diff changeset
  2081
     *
22566
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22108
diff changeset
  2082
     * @param s the stream to read
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2083
     * @throws InvalidObjectException always
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2084
     */
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20795
diff changeset
  2085
    private void readObject(ObjectInputStream s) throws InvalidObjectException {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2086
        throw new InvalidObjectException("Deserialization via serialization delegate");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2087
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2088
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2089
    void writeExternal(DataOutput out) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2090
        out.writeInt(year);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2091
        out.writeByte(month);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2092
        out.writeByte(day);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2093
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2094
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2095
    static LocalDate readExternal(DataInput in) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2096
        int year = in.readInt();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2097
        int month = in.readByte();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2098
        int dayOfMonth = in.readByte();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2099
        return LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2100
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2101
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2102
}