jdk/src/share/classes/java/time/temporal/Temporal.java
author rriggs
Wed, 04 Sep 2013 15:18:54 +0100
changeset 20519 eee7a92074fd
parent 19030 32f129cb6351
child 20520 0952771e3e25
permissions -rw-r--r--
8023762: Add ChronoPeriod interface and bind period to Chronology Summary: Make Period ISO-only, adding a Chronology-specific period concept Reviewed-by: sherman Contributed-by: scolebourne@joda.org
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * 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) 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.temporal;
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 java.time.DateTimeException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
 * Framework-level interface defining read-write access to a temporal object,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
 * such as a date, time, offset or some combination of these.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
 * This is the base interface type for date, time and offset objects that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
 * are complete enough to be manipulated using plus and minus.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
 * It is implemented by those classes that can provide and manipulate information
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
 * as {@linkplain TemporalField fields} or {@linkplain TemporalQuery queries}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
 * See {@link TemporalAccessor} for the read-only version of this interface.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
 * Most date and time information can be represented as a number.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
 * These are modeled using {@code TemporalField} with the number held using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
 * a {@code long} to handle large values. Year, month and day-of-month are
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
 * simple examples of fields, but they also include instant and offsets.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
 * See {@link ChronoField} for the standard set of fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 * Two pieces of date/time information cannot be represented by numbers,
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    83
 * the {@linkplain java.time.chrono.Chronology chronology} and the
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    84
 * {@linkplain java.time.ZoneId time-zone}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
 * These can be accessed via {@link #query(TemporalQuery) queries} using
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    86
 * the static methods defined on {@link TemporalQuery}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
 * This interface is a framework-level interface that should not be widely
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
 * used in application code. Instead, applications should create and pass
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
 * around instances of concrete types, such as {@code LocalDate}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 * There are many reasons for this, part of which is that implementations
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 * of this interface may be in calendar systems other than ISO.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    93
 * See {@link java.time.chrono.ChronoLocalDate} for a fuller discussion of the issues.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * <h3>When to implement</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * A class should implement this interface if it meets three criteria:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * <p><ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * <li>it provides access to date/time/offset information, as per {@code TemporalAccessor}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * <li>the set of fields are contiguous from the largest to the smallest
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * <li>the set of fields are complete, such that no other field is needed to define the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 *  valid range of values for the fields that are represented
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * </ul><p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * Four examples make this clear:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * <p><ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * <li>{@code LocalDate} implements this interface as it represents a set of fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 *  that are contiguous from days to forever and require no external information to determine
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 *  the validity of each date. It is therefore able to implement plus/minus correctly.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 * <li>{@code LocalTime} implements this interface as it represents a set of fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
 *  that are contiguous from nanos to within days and require no external information to determine
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 *  validity. It is able to implement plus/minus correctly, by wrapping around the day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 * <li>{@code MonthDay}, the combination of month-of-year and day-of-month, does not implement
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 *  this interface.  While the combination is contiguous, from days to months within years,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 *  the combination does not have sufficient information to define the valid range of values
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
 *  for day-of-month.  As such, it is unable to implement plus/minus correctly.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * <li>The combination day-of-week and day-of-month ("Friday the 13th") should not implement
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 *  this interface. It does not represent a contiguous set of fields, as days to weeks overlaps
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 *  days to months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 * </ul><p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   122
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
 * This interface places no restrictions on the mutability of implementations,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * however immutability is strongly recommended.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * All implementations must be {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
public interface Temporal extends TemporalAccessor {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   132
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   133
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   134
     * This checks if the specified unit can be added to, or subtracted from, this date-time.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   135
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   136
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   137
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   138
     * @implSpec
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   139
     * Implementations must check and handle all units defined in {@link ChronoUnit}.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   140
     * If the unit is supported, then true must be returned, otherwise false must be returned.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   141
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   142
     * If the field is not a {@code ChronoUnit}, then the result of this method
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   143
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   144
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   145
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   146
     * Implementations must ensure that no observable state is altered when this
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   147
     * read-only method is invoked.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   148
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   149
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   150
     * @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
   151
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   152
    boolean isSupported(TemporalUnit unit);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   153
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   154
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * Returns an adjusted object of the same type as this object with the adjustment made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     * This adjusts this date-time according to the rules of the specified adjuster.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * 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
   159
     * A more complex adjuster might set the date to the last day of the month.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   160
     * A selection of common adjustments is provided in {@link TemporalAdjuster}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * These include finding the "last day of the month" and "next Wednesday".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * The adjuster is responsible for handling special cases, such as the varying
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * lengths of month and leap years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * Some example code indicating how and why this method is used:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     *  date = date.with(Month.JULY);        // most key classes implement TemporalAdjuster
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     *  date = date.with(lastDayOfMonth());  // static import from Adjusters
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     *  date = date.with(next(WEDNESDAY));   // static import from Adjusters and DayOfWeek
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   172
     * @implSpec
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   173
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   174
     * Implementations must not alter either this object or the specified temporal object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     * Instead, an adjusted copy of the original must be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * This provides equivalent, safe behavior for immutable and mutable implementations.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * The default implementation must behave equivalent to this code:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     *  return adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * @param adjuster  the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * @return an object of the same type with the specified adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   188
    default Temporal with(TemporalAdjuster adjuster) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
        return adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
     * Returns an object of the same type as this object with the specified field altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
     * This returns a new object based on this one with the value for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * For example, on a {@code LocalDate}, this could be used to set the year, month or day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * The returned object will have the same observable type as this object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     * In some cases, changing a field is not fully defined. For example, if the target object is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     * a date representing the 31st January, then changing the month to February would be unclear.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
     * In cases like this, the field is responsible for resolving the result. Typically it will choose
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
     * 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
   203
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   204
     * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * Implementations must check and handle all fields defined in {@link ChronoField}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * If the field is supported, then the adjustment must be performed.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   207
     * If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * 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
   210
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * passing {@code this} as the first argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   213
     * Implementations must not alter this object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * Instead, an adjusted copy of the original must be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * This provides equivalent, safe behavior for immutable and mutable implementations.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * @return an object of the same type with the specified field set, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   221
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
    Temporal with(TemporalField field, long newValue);
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
     * Returns an object of the same type as this object with an amount added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   230
     * This adjusts this temporal, adding according to the rules of the specified amount.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   231
     * The amount is typically a {@link java.time.Period} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   232
     * the {@link TemporalAmount} interface, such as {@link java.time.Duration}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     * Some example code indicating how and why this method is used:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * <pre>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   236
     *  date = date.plus(period);                // add a Period instance
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   237
     *  date = date.plus(duration);              // add a Duration instance
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   238
     *  date = date.plus(workingDays(6));        // example user-written workingDays method
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * Note that calling {@code plus} followed by {@code minus} is not guaranteed to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * return the same date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   244
     * @implSpec
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   245
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   246
     * Implementations must not alter either this object or the specified temporal object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     * Instead, an adjusted copy of the original must be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * This provides equivalent, safe behavior for immutable and mutable implementations.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     * The default implementation must behave equivalent to this code:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     * <pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   252
     *  return amount.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   255
     * @param amount  the amount to add, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * @return an object of the same type with the specified adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * @throws DateTimeException if the addition cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   260
    default Temporal plus(TemporalAmount amount) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   261
        return amount.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * Returns an object of the same type as this object with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * This method returns a new object based on this one with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * For example, on a {@code LocalDate}, this could be used to add a number of years, months or days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * The returned object will have the same observable type as this object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * In some cases, changing a field is not fully defined. For example, if the target object is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * a date representing the 31st January, then adding one month would be unclear.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * In cases like this, the field is responsible for resolving the result. Typically it will choose
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     * 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
   275
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   276
     * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     * Implementations must check and handle all units defined in {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
     * If the unit is supported, then the addition must be performed.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   279
     * If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
     * If the unit is not a {@code ChronoUnit}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     * passing {@code this} as the first argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   285
     * Implementations must not alter this object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * Instead, an adjusted copy of the original must be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * This provides equivalent, safe behavior for immutable and mutable implementations.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * @param amountToAdd  the amount of the specified unit to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * @param unit  the unit of the period to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * @return an object of the same type with the specified period added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * @throws DateTimeException if the unit cannot be added
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   293
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
    Temporal plus(long amountToAdd, TemporalUnit unit);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * Returns an object of the same type as this object with an amount subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   302
     * This adjusts this temporal, subtracting according to the rules of the specified amount.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   303
     * The amount is typically a {@link java.time.Period} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   304
     * the {@link TemporalAmount} interface, such as {@link java.time.Duration}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     * Some example code indicating how and why this method is used:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
     * <pre>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   308
     *  date = date.minus(period);               // subtract a Period instance
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   309
     *  date = date.minus(duration);             // subtract a Duration instance
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   310
     *  date = date.minus(workingDays(6));       // example user-written workingDays method
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * Note that calling {@code plus} followed by {@code minus} is not guaranteed to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * return the same date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   316
     * @implSpec
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   317
     * <p>
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   318
     * Implementations must not alter either this object or the specified temporal object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
     * Instead, an adjusted copy of the original must be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     * This provides equivalent, safe behavior for immutable and mutable implementations.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * The default implementation must behave equivalent to this code:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * <pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   324
     *  return amount.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   327
     * @param amount  the amount to subtract, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
     * @return an object of the same type with the specified adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * @throws DateTimeException if the subtraction cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   332
    default Temporal minus(TemporalAmount amount) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   333
        return amount.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * Returns an object of the same type as this object with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     * This method returns a new object based on this one with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
     * For example, on a {@code LocalDate}, this could be used to subtract a number of years, months or days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * The returned object will have the same observable type as this object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     * In some cases, changing a field is not fully defined. For example, if the target object is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
     * a date representing the 31st March, then subtracting one month would be unclear.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
     * In cases like this, the field is responsible for resolving the result. Typically it will choose
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
     * 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
   347
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   348
     * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * Implementations must behave in a manor equivalent to the default method behavior.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
     * <p>
20519
eee7a92074fd 8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents: 19030
diff changeset
   351
     * Implementations must not alter this object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     * Instead, an adjusted copy of the original must be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     * This provides equivalent, safe behavior for immutable and mutable implementations.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * The default implementation must behave equivalent to this code:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     *  return (amountToSubtract == Long.MIN_VALUE ?
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     *      plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * @param amountToSubtract  the amount of the specified unit to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * @param unit  the unit of the period to subtract, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     * @return an object of the same type with the specified period subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
     * @throws DateTimeException if the unit cannot be subtracted
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   365
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   368
    default Temporal minus(long amountToSubtract, TemporalUnit unit) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
        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
   370
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   374
     * Calculates the amount of time until another temporal in terms of the specified unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   376
     * This calculates the amount of time between two temporal objects
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   377
     * of the same type in terms of a single {@code TemporalUnit}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
     * The start and end points are {@code this} and the specified temporal.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
     * The result will be negative if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
     * For example, the period in hours between two temporal objects can be
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   381
     * calculated using {@code startTime.until(endTime, HOURS)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * complete units between the two temporals.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     * For example, the period in hours between the times 11:30 and 13:29
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     * will only be one hour as it is one minute short of two hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   388
     * There are two equivalent ways of using this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   389
     * The first is to invoke this method directly.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   390
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     * <pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   392
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   393
     *   temporal = start.until(end, unit);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   394
     *   temporal = unit.between(start, end);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   395
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   396
     * 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
   397
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   398
     * For example, this method allows the number of days between two dates to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   399
     * be calculated:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   400
     * <pre>
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   401
     *  long daysBetween = start.until(end, DAYS);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   402
     *  // or alternatively
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   403
     *  long daysBetween = DAYS.between(start, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   406
     * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
     * Implementations must begin by checking to ensure that the input temporal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * object is of the same observable type as the implementation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * They must then perform the calculation for all instances of {@link ChronoUnit}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   410
     * An {@code UnsupportedTemporalTypeException} must be thrown for {@code ChronoUnit}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * instances that are unsupported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * 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
   414
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * passing {@code this} as the first argument and the input temporal as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * the second argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * In summary, implementations must behave in a manner equivalent to this code:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     *  // check input temporal is the same type as this class
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     *  if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     *    // if unit is supported, then calculate and return result
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   423
     *    // else throw UnsupportedTemporalTypeException for unsupported units
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
     *  }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   425
     *  return unit.between(this, endTemporal);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     * <p>
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   428
     * Implementations must ensure that no observable state is altered when this
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   429
     * read-only method is invoked.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     * @param endTemporal  the end temporal, of the same type as this object, not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   432
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   433
     * @return the amount of time between this temporal object and the specified one
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   434
     *  in terms of the unit; positive if the specified object is later than this one,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   435
     *  negative if it is earlier than this one
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   436
     * @throws DateTimeException if the amount cannot be calculated
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   437
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     */
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   440
    long until(Temporal endTemporal, TemporalUnit unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
}