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