jdk/src/share/classes/java/time/ZonedDateTime.java
author mduigou
Tue, 05 Nov 2013 19:44:41 -0800
changeset 21615 0231a565a5b7
parent 20873 e91d5b1cb8e6
child 22081 86eb26ff8f2b
permissions -rw-r--r--
8021309: replace test/Makefile jdk_* targets with jtreg groups 8015068: Use jtreg -exclude for handling problemList.txt exclusions Reviewed-by: jjg, smarks, chegar, alanb, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.io.DataOutput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import java.io.IOException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import java.io.InvalidObjectException;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    71
import java.io.ObjectInput;
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
    72
import java.io.InvalidObjectException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    74
import java.time.chrono.ChronoZonedDateTime;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.time.format.DateTimeFormatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.temporal.TemporalAdjuster;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    82
import java.time.temporal.TemporalAmount;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.temporal.TemporalField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    86
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.time.zone.ZoneOffsetTransition;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.time.zone.ZoneRules;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * A date-time with a time-zone in the ISO-8601 calendar system,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * such as {@code 2007-12-03T10:15:30+01:00 Europe/Paris}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * {@code ZonedDateTime} is an immutable representation of a date-time with a time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * This class stores all date and time fields, to a precision of nanoseconds,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * and a time-zone, with a zone offset used to handle ambiguous local date-times.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * For example, the value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * "2nd October 2007 at 13:45.30.123456789 +02:00 in the Europe/Paris time-zone"
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * can be stored in a {@code ZonedDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * This class handles conversion from the local time-line of {@code LocalDateTime}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * to the instant time-line of {@code Instant}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * The difference between the two time-lines is the offset from UTC/Greenwich,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * represented by a {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 * Converting between the two time-lines involves calculating the offset using the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 * {@link ZoneRules rules} accessed from the {@code ZoneId}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
 * Obtaining the offset for an instant is simple, as there is exactly one valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 * offset for each instant. By contrast, obtaining the offset for a local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 * is not straightforward. There are three cases:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20747
diff changeset
   114
 * <ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * <li>Normal, with one valid offset. For the vast majority of the year, the normal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
 *  case applies, where there is a single valid offset for the local date-time.</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * <li>Gap, with zero valid offsets. This is when clocks jump forward typically
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 *  due to the spring daylight savings change from "winter" to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 *  In a gap there are local date-time values with no valid offset.</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 * <li>Overlap, with two valid offsets. This is when clocks are set back typically
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 *  due to the autumn daylight savings change from "summer" to "winter".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
 *  In an overlap there are local date-time values with two valid offsets.</li>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20747
diff changeset
   123
 * </ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * Any method that converts directly or implicitly from a local date-time to an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 * instant by obtaining the offset has the potential to be complicated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
 * For Gaps, the general strategy is that if the local date-time falls in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
 * middle of a Gap, then the resulting zoned date-time will have a local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
 * shifted forwards by the length of the Gap, resulting in a date-time in the later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
 * offset, typically "summer" time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
 * For Overlaps, the general strategy is that if the local date-time falls in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
 * middle of an Overlap, then the previous offset will be retained. If there is no
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
 * previous offset, or the previous offset is invalid, then the earlier offset is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
 * used, typically "summer" time.. Two additional methods,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
 * {@link #withEarlierOffsetAtOverlap()} and {@link #withLaterOffsetAtOverlap()},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
 * help manage the case of an overlap.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   139
 * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   140
 * In terms of design, this class should be viewed primarily as the combination
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   141
 * of a {@code LocalDateTime} and a {@code ZoneId}. The {@code ZoneOffset} is
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   142
 * a vital, but secondary, piece of information, used to ensure that the class
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   143
 * represents an instant, especially during a daylight savings overlap.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   145
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
 * A {@code ZonedDateTime} holds state equivalent to three separate objects,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
 * a {@code LocalDateTime}, a {@code ZoneId} and the resolved {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
 * The offset and local date-time are used to define an instant when necessary.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
 * The zone ID is used to obtain the rules for how and when the offset changes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
 * The offset cannot be freely set, as the zone controls which offsets are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
public final class ZonedDateTime
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   157
        implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
    private static final long serialVersionUID = -6260982410461394882L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * The local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
    private final LocalDateTime dateTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     * The offset from UTC/Greenwich.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    private final ZoneOffset offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     * The time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
    private final ZoneId zone;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * Obtains the current date-time from the system clock in the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     * This will query the {@link Clock#systemDefaultZone() system clock} in the default
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     * time-zone to obtain the current date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * The zone and offset will be set based on the time-zone in the clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * Using this method will prevent the ability to use an alternate clock for testing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     * @return the current date-time using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
    public static ZonedDateTime now() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
        return now(Clock.systemDefaultZone());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
     * Obtains the current date-time from the system clock in the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * Specifying the time-zone avoids dependence on the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     * The offset will be calculated from the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
     * Using this method will prevent the ability to use an alternate clock for testing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * @param zone  the zone ID to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * @return the current date-time using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    public static ZonedDateTime now(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        return now(Clock.system(zone));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * Obtains the current date-time from the specified clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * This will query the specified clock to obtain the current date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * The zone and offset will be set based on the time-zone in the clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * Using this method allows the use of an alternate clock for testing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * The alternate clock may be introduced using {@link Clock dependency injection}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * @param clock  the clock to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * @return the current date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
    public static ZonedDateTime now(Clock clock) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
        Objects.requireNonNull(clock, "clock");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
        final Instant now = clock.instant();  // called once
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
        return ofInstant(now, clock.getZone());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   231
     * Obtains an instance of {@code ZonedDateTime} from a local date and time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   232
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   233
     * This creates a zoned date-time matching the input local date and time as closely as possible.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   234
     * Time-zone rules, such as daylight savings, mean that not every local date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   235
     * is valid for the specified zone, thus the local date-time may be adjusted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   236
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   237
     * The local date time and first combined to form a local date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   238
     * The local date-time is then resolved to a single instant on the time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   239
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   240
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   241
     *<p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   242
     * In most cases, there is only one valid offset for a local date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   243
     * In the case of an overlap, when clocks are set back, there are two valid offsets.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   244
     * This method uses the earlier offset typically corresponding to "summer".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   245
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   246
     * In the case of a gap, when clocks jump forward, there is no valid offset.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   247
     * Instead, the local date-time is adjusted to be later by the length of the gap.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   248
     * For a typical one hour daylight savings change, the local date-time will be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
     * moved one hour later into the offset typically corresponding to "summer".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   250
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   251
     * @param date  the local date, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   252
     * @param time  the local time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   253
     * @param zone  the time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   254
     * @return the offset date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   255
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   256
    public static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   257
        return of(LocalDateTime.of(date, time), zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   259
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   260
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * Obtains an instance of {@code ZonedDateTime} from a local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * This creates a zoned date-time matching the input local date-time as closely as possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * Time-zone rules, such as daylight savings, mean that not every local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * is valid for the specified zone, thus the local date-time may be adjusted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * The local date-time is resolved to a single instant on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     *<p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * In most cases, there is only one valid offset for a local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * In the case of an overlap, when clocks are set back, there are two valid offsets.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * This method uses the earlier offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
     * In the case of a gap, when clocks jump forward, there is no valid offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
     * Instead, the local date-time is adjusted to be later by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     * For a typical one hour daylight savings change, the local date-time will be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
     * moved one hour later into the offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     * @param localDateTime  the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
    public static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
        return ofLocal(localDateTime, zone, null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   289
     * Obtains an instance of {@code ZonedDateTime} from a year, month, day,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   290
     * hour, minute, second, nanosecond and time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   291
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   292
     * This creates a zoned date-time matching the local date-time of the seven
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   293
     * specified fields as closely as possible.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   294
     * Time-zone rules, such as daylight savings, mean that not every local date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   295
     * is valid for the specified zone, thus the local date-time may be adjusted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   296
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   297
     * The local date-time is resolved to a single instant on the time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   298
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   299
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   300
     *<p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   301
     * In most cases, there is only one valid offset for a local date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   302
     * In the case of an overlap, when clocks are set back, there are two valid offsets.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   303
     * This method uses the earlier offset typically corresponding to "summer".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   304
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   305
     * In the case of a gap, when clocks jump forward, there is no valid offset.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   306
     * Instead, the local date-time is adjusted to be later by the length of the gap.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   307
     * For a typical one hour daylight savings change, the local date-time will be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   308
     * moved one hour later into the offset typically corresponding to "summer".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   309
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   310
     * This method exists primarily for writing test cases.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   311
     * Non test-code will typically use other methods to create an offset time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   312
     * {@code LocalDateTime} has five additional convenience variants of the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   313
     * equivalent factory method taking fewer arguments.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   314
     * They are not provided here to reduce the footprint of the API.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   315
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   316
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   317
     * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   318
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   319
     * @param hour  the hour-of-day to represent, from 0 to 23
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   320
     * @param minute  the minute-of-hour to represent, from 0 to 59
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   321
     * @param second  the second-of-minute to represent, from 0 to 59
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   322
     * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   323
     * @param zone  the time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   324
     * @return the offset date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   325
     * @throws DateTimeException if the value of any field is out of range, or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   326
     *  if the day-of-month is invalid for the month-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   327
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   328
    public static ZonedDateTime of(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   329
            int year, int month, int dayOfMonth,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   330
            int hour, int minute, int second, int nanoOfSecond, ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   331
        LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   332
        return ofLocal(dt, zone, null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   333
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   334
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   335
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * Obtains an instance of {@code ZonedDateTime} from a local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * using the preferred offset if possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     * The local date-time is resolved to a single instant on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     *<p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     * In most cases, there is only one valid offset for a local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
     * In the case of an overlap, where clocks are set back, there are two valid offsets.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
     * If the preferred offset is one of the valid offsets then it is used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
     * Otherwise the earlier valid offset is used, typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
     * In the case of a gap, where clocks jump forward, there is no valid offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * Instead, the local date-time is adjusted to be later by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
     * For a typical one hour daylight savings change, the local date-time will be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
     * moved one hour later into the offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     * @param localDateTime  the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * @param preferredOffset  the zone offset, null if no preference
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
    public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
        Objects.requireNonNull(localDateTime, "localDateTime");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
        if (zone instanceof ZoneOffset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
            return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
        ZoneRules rules = zone.getRules();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
        List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
        ZoneOffset offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
        if (validOffsets.size() == 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
            offset = validOffsets.get(0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
        } else if (validOffsets.size() == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
            ZoneOffsetTransition trans = rules.getTransition(localDateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
            localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
            offset = trans.getOffsetAfter();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
            if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
                offset = preferredOffset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
                offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
        return new ZonedDateTime(localDateTime, offset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     * Obtains an instance of {@code ZonedDateTime} from an {@code Instant}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * This creates a zoned date-time with the same instant as that specified.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * Calling {@link #toInstant()} will return an instant equal to the one used here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     * Converting an instant to a zoned date-time is simple as there is only one valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     * offset for each instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * @param instant  the instant to create the date-time from, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * @throws DateTimeException if the result exceeds the supported range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
    public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
        Objects.requireNonNull(instant, "instant");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
        return create(instant.getEpochSecond(), instant.getNano(), zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     * Obtains an instance of {@code ZonedDateTime} from the instant formed by combining
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
     * the local date-time and offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * This creates a zoned date-time by {@link LocalDateTime#toInstant(ZoneOffset) combining}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * the {@code LocalDateTime} and {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * This combination uniquely specifies an instant without ambiguity.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * Converting an instant to a zoned date-time is simple as there is only one valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * offset for each instant. If the valid offset is different to the offset specified,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * the the date-time and offset of the zoned date-time will differ from those specified.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * If the {@code ZoneId} to be used is a {@code ZoneOffset}, this method is equivalent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * to {@link #of(LocalDateTime, ZoneId)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * @param localDateTime  the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     * @param offset  the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
    public static ZonedDateTime ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
        Objects.requireNonNull(localDateTime, "localDateTime");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
        Objects.requireNonNull(offset, "offset");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
        Objects.requireNonNull(zone, "zone");
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   428
        if (zone.getRules().isValidOffset(localDateTime, offset)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   429
            return new ZonedDateTime(localDateTime, offset, zone);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   430
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
        return create(localDateTime.toEpochSecond(offset), localDateTime.getNano(), zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * Obtains an instance of {@code ZonedDateTime} using seconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * @throws DateTimeException if the result exceeds the supported range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
    private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
        ZoneRules rules = zone.getRules();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
        Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);  // TODO: rules should be queryable by epochSeconds
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
        ZoneOffset offset = rules.getOffset(instant);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
        LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
        return new ZonedDateTime(ldt, offset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
     * Obtains an instance of {@code ZonedDateTime} strictly validating the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
     * combination of local date-time, offset and zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
     * This creates a zoned date-time ensuring that the offset is valid for the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
     * local date-time according to the rules of the specified zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     * If the offset is invalid, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
     * @param localDateTime  the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
     * @param offset  the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
    public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
        Objects.requireNonNull(localDateTime, "localDateTime");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
        Objects.requireNonNull(offset, "offset");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
        ZoneRules rules = zone.getRules();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
        if (rules.isValidOffset(localDateTime, offset) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
            ZoneOffsetTransition trans = rules.getTransition(localDateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
            if (trans != null && trans.isGap()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
                // error message says daylight savings for simplicity
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
                // even though there are other kinds of gaps
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
                throw new DateTimeException("LocalDateTime '" + localDateTime +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
                        "' does not exist in zone '" + zone +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
                        "' due to a gap in the local time-line, typically caused by daylight savings");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
            throw new DateTimeException("ZoneOffset '" + offset + "' is not valid for LocalDateTime '" +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
                    localDateTime + "' in zone '" + zone + "'");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
        return new ZonedDateTime(localDateTime, offset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     * Obtains an instance of {@code ZonedDateTime} leniently, for advanced use cases,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     * allowing any combination of local date-time, offset and zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
     * This creates a zoned date-time with no checks other than no nulls.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
     * This means that the resulting zoned date-time may have an offset that is in conflict
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
     * with the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
     * This method is intended for advanced use cases.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
     * For example, consider the case where a zoned date-time with valid fields is created
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
     * and then stored in a database or serialization-based store. At some later point,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
     * the object is then re-loaded. However, between those points in time, the government
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
     * that defined the time-zone has changed the rules, such that the originally stored
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
     * local date-time now does not occur. This method can be used to create the object
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * in an "invalid" state, despite the change in rules.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     * @param localDateTime  the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * @param offset  the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * @param zone  the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
    private static ZonedDateTime ofLenient(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
        Objects.requireNonNull(localDateTime, "localDateTime");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
        Objects.requireNonNull(offset, "offset");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
        if (zone instanceof ZoneOffset && offset.equals(zone) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
            throw new IllegalArgumentException("ZoneId must match ZoneOffset");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
        return new ZonedDateTime(localDateTime, offset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
     * Obtains an instance of {@code ZonedDateTime} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   521
     * This obtains a zoned date-time based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   522
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   523
     * which this factory converts to an instance of {@code ZonedDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   525
     * The conversion will first obtain a {@code ZoneId} from the temporal object,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   526
     * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   527
     * an {@code Instant}, falling back to a {@code LocalDateTime} if necessary.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   528
     * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   529
     * with {@code Instant} or {@code LocalDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   530
     * Implementations are permitted to perform optimizations such as accessing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   531
     * those fields that are equivalent to the relevant objects.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
     * allowing it to be used in queries via method reference, {@code ZonedDateTime::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     * @throws DateTimeException if unable to convert to an {@code ZonedDateTime}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
    public static ZonedDateTime from(TemporalAccessor temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
        if (temporal instanceof ZonedDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
            return (ZonedDateTime) temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
        try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
            ZoneId zone = ZoneId.from(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
            try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
                long epochSecond = temporal.getLong(INSTANT_SECONDS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
                int nanoOfSecond = temporal.get(NANO_OF_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
                return create(epochSecond, nanoOfSecond, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
            } catch (DateTimeException ex1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
                LocalDateTime ldt = LocalDateTime.from(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
                return of(ldt, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
        } catch (DateTimeException ex) {
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   556
            throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " +
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   557
                    temporal + " of type " + temporal.getClass().getName(), ex);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   563
     * Obtains an instance of {@code ZonedDateTime} from a text string such as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
     * {@code 2007-12-03T10:15:30+01:00[Europe/Paris]}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
     * The string must represent a valid date-time and is parsed using
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   567
     * {@link java.time.format.DateTimeFormatter#ISO_ZONED_DATE_TIME}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   569
     * @param text  the text to parse such as "2007-12-03T10:15:30+01:00[Europe/Paris]", not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   570
     * @return the parsed zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
    public static ZonedDateTime parse(CharSequence text) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   574
        return parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   578
     * Obtains an instance of {@code ZonedDateTime} from a text string using a specific formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
     * The text is parsed using the formatter, returning a date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   581
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * @param formatter  the formatter to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
     * @return the parsed zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
    public static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
        return formatter.parse(text, ZonedDateTime::from);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   591
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
     * @param dateTime  the date-time, validated as not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
     * @param offset  the zone offset, validated as not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
     * @param zone  the time-zone, validated as not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
    private ZonedDateTime(LocalDateTime dateTime, ZoneOffset offset, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
        this.dateTime = dateTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
        this.offset = offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
        this.zone = zone;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   607
     * Resolves the new local date-time using this zone ID, retaining the offset if possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   608
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
     * @param newDateTime  the new local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
    private ZonedDateTime resolveLocal(LocalDateTime newDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
        return ofLocal(newDateTime, zone, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   614
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   615
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
     * Resolves the new local date-time using the offset to identify the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
     * @param newDateTime  the new local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
    private ZonedDateTime resolveInstant(LocalDateTime newDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
        return ofInstant(newDateTime, offset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   627
     * Resolves the offset into this zoned date-time for the with methods.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   629
     * This typically ignores the offset, unless it can be used to switch offset in a DST overlap.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
     * @param offset  the offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
     * @return the zoned date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
    private ZonedDateTime resolveOffset(ZoneOffset offset) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   635
        if (offset.equals(this.offset) == false && zone.getRules().isValidOffset(dateTime, offset)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   636
            return new ZonedDateTime(dateTime, offset, zone);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   637
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   638
        return this;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
     * 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
   646
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   647
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   648
     * methods will throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
     * The supported fields are:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
     * <li>{@code NANO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
     * <li>{@code NANO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
     * <li>{@code MICRO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
     * <li>{@code MICRO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
     * <li>{@code MILLI_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
     * <li>{@code MILLI_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
     * <li>{@code SECOND_OF_MINUTE}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
     * <li>{@code SECOND_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
     * <li>{@code MINUTE_OF_HOUR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
     * <li>{@code MINUTE_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     * <li>{@code HOUR_OF_AMPM}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     * <li>{@code CLOCK_HOUR_OF_AMPM}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * <li>{@code HOUR_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * <li>{@code CLOCK_HOUR_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     * <li>{@code AMPM_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     * <li>{@code DAY_OF_WEEK}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
     * <li>{@code DAY_OF_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
     * <li>{@code DAY_OF_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
     * <li>{@code EPOCH_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
     * <li>{@code ALIGNED_WEEK_OF_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     * <li>{@code ALIGNED_WEEK_OF_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     * <li>{@code MONTH_OF_YEAR}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   677
     * <li>{@code PROLEPTIC_MONTH}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
     * <li>{@code YEAR_OF_ERA}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
     * <li>{@code YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
     * <li>{@code ERA}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
     * <li>{@code INSTANT_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
     * <li>{@code OFFSET_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   687
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     * @return true if the field is supported on this date-time, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
    public boolean isSupported(TemporalField field) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   696
        return field instanceof ChronoField || (field != null && field.isSupportedBy(this));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   700
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   701
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   702
     * 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
   703
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   704
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   705
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   706
     * 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
   707
     * The supported units are:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   708
     * <ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   709
     * <li>{@code NANOS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   710
     * <li>{@code MICROS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   711
     * <li>{@code MILLIS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   712
     * <li>{@code SECONDS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   713
     * <li>{@code MINUTES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   714
     * <li>{@code HOURS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   715
     * <li>{@code HALF_DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   716
     * <li>{@code DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   717
     * <li>{@code WEEKS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   718
     * <li>{@code MONTHS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   719
     * <li>{@code YEARS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   720
     * <li>{@code DECADES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   721
     * <li>{@code CENTURIES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   722
     * <li>{@code MILLENNIA}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   723
     * <li>{@code ERAS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   724
     * </ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   725
     * All other {@code ChronoUnit} instances will return false.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   726
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   727
     * 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
   728
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   729
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   730
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   731
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   732
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   733
     * @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
   734
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   735
    @Override  // override for Javadoc
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   736
    public boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   737
        return ChronoZonedDateTime.super.isSupported(unit);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   738
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   739
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   740
    //-----------------------------------------------------------------------
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   741
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   742
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
     * This date-time is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
     * If it is not possible to return the range, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
     * The {@link #isSupported(TemporalField) supported fields} will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
     * appropriate range instances.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   752
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   753
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   755
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
     * @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
   762
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
            if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
                return field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   769
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   770
            return dateTime.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   772
        return field.rangeRefinedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
     * Gets the value of the specified field from this date-time as an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
     * This queries this date-time for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * The returned value will always be within the valid range of values for the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
     * 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
   786
     * {@code EPOCH_DAY}, {@code PROLEPTIC_MONTH} and {@code INSTANT_SECONDS} which are too
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
     * large to fit in an {@code int} and throw a {@code DateTimeException}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   788
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   791
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
     * @return the value for the field
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   797
     * @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
   798
     *         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
   799
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   800
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   803
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   804
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   805
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   806
            switch ((ChronoField) field) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   807
                case INSTANT_SECONDS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   808
                    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
   809
                case OFFSET_SECONDS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   810
                    return getOffset().getTotalSeconds();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
            return dateTime.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
        return ChronoZonedDateTime.super.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
     * Gets the value of the specified field from this date-time as a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   819
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
     * This queries this date-time for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   823
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   824
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   825
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   826
     * values based on this date-time.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   827
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   828
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   829
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   830
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   832
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   835
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   836
     * @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
   837
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   841
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   842
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
            switch ((ChronoField) field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
                case INSTANT_SECONDS: return toEpochSecond();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
                case OFFSET_SECONDS: return getOffset().getTotalSeconds();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
            return dateTime.getLong(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   849
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   850
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
     * Gets the zone offset, such as '+01:00'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
     * This is the offset of the local date-time from UTC/Greenwich.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
     * @return the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
    public ZoneOffset getOffset() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
        return offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
     * Returns a copy of this date-time changing the zone offset to the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
     * earlier of the two valid offsets at a local time-line overlap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
     * This method only has any effect when the local time-line overlaps, such as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
     * at an autumn daylight savings cutover. In this scenario, there are two
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
     * valid offsets for the local date-time. Calling this method will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
     * a zoned date-time with the earlier of the two selected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
     * If this method is called when it is not an overlap, {@code this}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
     * is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   876
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
     * @return a {@code ZonedDateTime} based on this date-time with the earlier offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
    public ZonedDateTime withEarlierOffsetAtOverlap() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
        ZoneOffsetTransition trans = getZone().getRules().getTransition(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
        if (trans != null && trans.isOverlap()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
            ZoneOffset earlierOffset = trans.getOffsetBefore();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
            if (earlierOffset.equals(offset) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
                return new ZonedDateTime(dateTime, earlierOffset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
     * Returns a copy of this date-time changing the zone offset to the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
     * later of the two valid offsets at a local time-line overlap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     * This method only has any effect when the local time-line overlaps, such as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * at an autumn daylight savings cutover. In this scenario, there are two
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     * valid offsets for the local date-time. Calling this method will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
     * a zoned date-time with the later of the two selected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
     * If this method is called when it is not an overlap, {@code this}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
     * is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
     * @return a {@code ZonedDateTime} based on this date-time with the later offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
    public ZonedDateTime withLaterOffsetAtOverlap() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   911
        ZoneOffsetTransition trans = getZone().getRules().getTransition(toLocalDateTime());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
        if (trans != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
            ZoneOffset laterOffset = trans.getOffsetAfter();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
            if (laterOffset.equals(offset) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
                return new ZonedDateTime(dateTime, laterOffset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
     * Gets the time-zone, such as 'Europe/Paris'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
     * This returns the zone ID. This identifies the time-zone {@link ZoneRules rules}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
     * that determine when and how the offset from UTC/Greenwich changes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
     * The zone ID may be same as the {@linkplain #getOffset() offset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
     * If this is true, then any future calculations, such as addition or subtraction,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
     * have no complex edge cases due to time-zone rules.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   931
     * See also {@link #withFixedOffsetZone()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   932
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   933
     * @return the time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   934
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   935
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   936
    public ZoneId getZone() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   937
        return zone;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   938
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   939
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   940
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   941
     * Returns a copy of this date-time with a different time-zone,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   942
     * retaining the local date-time if possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   943
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   944
     * This method changes the time-zone and retains the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   945
     * The local date-time is only changed if it is invalid for the new zone,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
     * determined using the same approach as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
     * {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   948
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   949
     * To change the zone and adjust the local date-time,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   950
     * use {@link #withZoneSameInstant(ZoneId)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   951
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   952
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   954
     * @param zone  the time-zone to change to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   955
     * @return a {@code ZonedDateTime} based on this date-time with the requested zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   956
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   957
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
    public ZonedDateTime withZoneSameLocal(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   960
        return this.zone.equals(zone) ? this : ofLocal(dateTime, zone, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   961
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   962
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   963
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   964
     * Returns a copy of this date-time with a different time-zone,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   965
     * retaining the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   966
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   967
     * This method changes the time-zone and retains the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
     * This normally results in a change to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   969
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   970
     * This method is based on retaining the same instant, thus gaps and overlaps
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   971
     * in the local time-line have no effect on the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   972
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     * To change the offset while keeping the local time,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
     * use {@link #withZoneSameLocal(ZoneId)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
     * @param zone  the time-zone to change to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
     * @return a {@code ZonedDateTime} based on this date-time with the requested zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
    public ZonedDateTime withZoneSameInstant(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
        return this.zone.equals(zone) ? this :
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
            create(dateTime.toEpochSecond(offset), dateTime.getNano(), zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   987
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     * Returns a copy of this date-time with the zone ID set to the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
     * This returns a zoned date-time where the zone ID is the same as {@link #getOffset()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
     * The local date-time, offset and instant of the result will be the same as in this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
     * Setting the date-time to a fixed single offset means that any future
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
     * calculations, such as addition or subtraction, have no complex edge cases
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
     * due to time-zone rules.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
     * This might also be useful when sending a zoned date-time across a network,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
     * as most protocols, such as ISO-8601, only handle offsets,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   998
     * and not region-based zone IDs.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1000
     * This is equivalent to {@code ZonedDateTime.of(zdt.toLocalDateTime(), zdt.getOffset())}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
     * @return a {@code ZonedDateTime} with the zone ID set to the offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
    public ZonedDateTime withFixedOffsetZone() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
        return this.zone.equals(offset) ? this : new ZonedDateTime(dateTime, offset, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
     * Gets the {@code LocalDateTime} part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
     * This returns a {@code LocalDateTime} with the same year, month, day and time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
     * as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
     * @return the local date-time part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
    @Override  // override for return type
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1018
    public LocalDateTime toLocalDateTime() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
        return dateTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1021
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
     * Gets the {@code LocalDate} part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
     * This returns a {@code LocalDate} with the same year, month and day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
     * as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
     * @return the date part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
    @Override  // override for return type
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1032
    public LocalDate toLocalDate() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1033
        return dateTime.toLocalDate();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     * Gets the year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
     * This method returns the primitive {@code int} value for the year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
     * The year returned by this method is proleptic as per {@code get(YEAR)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1042
     * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
     * @return the year, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
    public int getYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
        return dateTime.getYear();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
     * Gets the month-of-year field from 1 to 12.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1053
     * This method returns the month as an {@code int} from 1 to 12.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1054
     * Application code is frequently clearer if the enum {@link Month}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
     * is used by calling {@link #getMonth()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1056
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1057
     * @return the month-of-year, from 1 to 12
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1058
     * @see #getMonth()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1059
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
    public int getMonthValue() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1061
        return dateTime.getMonthValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
     * Gets the month-of-year field using the {@code Month} enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
     * This method returns the enum {@link Month} for the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
     * This avoids confusion as to what {@code int} values mean.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
     * If you need access to the primitive {@code int} value then the enum
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
     * provides the {@link Month#getValue() int value}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
     * @return the month-of-year, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
     * @see #getMonthValue()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
    public Month getMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
        return dateTime.getMonth();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
     * Gets the day-of-month field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
     * This method returns the primitive {@code int} value for the day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
     * @return the day-of-month, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
    public int getDayOfMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
        return dateTime.getDayOfMonth();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1090
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
     * Gets the day-of-year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
     * This method returns the primitive {@code int} value for the day-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1094
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1095
     * @return the day-of-year, from 1 to 365, or 366 in a leap year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1096
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1097
    public int getDayOfYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
        return dateTime.getDayOfYear();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
     * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1104
     * This method returns the enum {@link DayOfWeek} for the day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1105
     * This avoids confusion as to what {@code int} values mean.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
     * If you need access to the primitive {@code int} value then the enum
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1107
     * provides the {@link DayOfWeek#getValue() int value}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1108
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1109
     * Additional information can be obtained from the {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1110
     * This includes textual names of the values.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1112
     * @return the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1113
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1114
    public DayOfWeek getDayOfWeek() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1115
        return dateTime.getDayOfWeek();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1116
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1117
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1118
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1120
     * Gets the {@code LocalTime} part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1121
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1122
     * This returns a {@code LocalTime} with the same hour, minute, second and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
     * nanosecond as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1124
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
     * @return the time part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1126
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
    @Override  // override for Javadoc and performance
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1128
    public LocalTime toLocalTime() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1129
        return dateTime.toLocalTime();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1132
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
     * Gets the hour-of-day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1134
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1135
     * @return the hour-of-day, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1136
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1137
    public int getHour() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1138
        return dateTime.getHour();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1139
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1142
     * Gets the minute-of-hour field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1143
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1144
     * @return the minute-of-hour, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1145
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1146
    public int getMinute() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
        return dateTime.getMinute();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1148
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1149
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1150
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1151
     * Gets the second-of-minute field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1152
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1153
     * @return the second-of-minute, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1154
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1155
    public int getSecond() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1156
        return dateTime.getSecond();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1157
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1159
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1160
     * Gets the nano-of-second field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1161
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1162
     * @return the nano-of-second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1163
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1164
    public int getNano() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1165
        return dateTime.getNano();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1166
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1167
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1168
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1169
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1170
     * Returns an adjusted copy of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1171
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1172
     * This returns a {@code ZonedDateTime}, based on this one, with the date-time adjusted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1173
     * The adjustment takes place using the specified adjuster strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1174
     * Read the documentation of the adjuster to understand what adjustment will be made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1175
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1176
     * A simple adjuster might simply set the one of the fields, such as the year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1177
     * A more complex adjuster might set the date to the last day of the month.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1178
     * A selection of common adjustments is provided in {@link TemporalAdjuster}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1179
     * These include finding the "last day of the month" and "next Wednesday".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1180
     * Key date-time classes also implement the {@code TemporalAdjuster} interface,
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1181
     * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1182
     * The adjuster is responsible for handling special cases, such as the varying
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1183
     * lengths of month and leap years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1184
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1185
     * For example this code returns a date on the last day of July:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1186
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1187
     *  import static java.time.Month.*;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1188
     *  import static java.time.temporal.Adjusters.*;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1189
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1190
     *  result = zonedDateTime.with(JULY).with(lastDayOfMonth());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1191
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1192
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1193
     * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1194
     * thus this method can be used to change the date, time or offset:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1195
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1196
     *  result = zonedDateTime.with(date);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1197
     *  result = zonedDateTime.with(time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1198
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1199
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1200
     * {@link ZoneOffset} also implements {@code TemporalAdjuster} however using it
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1201
     * as an argument typically has no effect. The offset of a {@code ZonedDateTime} is
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1202
     * controlled primarily by the time-zone. As such, changing the offset does not generally
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1203
     * make sense, because there is only one valid offset for the local date-time and zone.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1204
     * If the zoned date-time is in a daylight savings overlap, then the offset is used
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1205
     * to switch between the two valid offsets. In all other cases, the offset is ignored.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1206
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1207
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1208
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1209
     * specified adjuster passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1210
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1211
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1212
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1213
     * @param adjuster the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1214
     * @return a {@code ZonedDateTime} based on {@code this} with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1215
     * @throws DateTimeException if the adjustment cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1216
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1217
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1218
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1219
    public ZonedDateTime with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1220
        // optimizations
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1221
        if (adjuster instanceof LocalDate) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1222
            return resolveLocal(LocalDateTime.of((LocalDate) adjuster, dateTime.toLocalTime()));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1223
        } else if (adjuster instanceof LocalTime) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1224
            return resolveLocal(LocalDateTime.of(dateTime.toLocalDate(), (LocalTime) adjuster));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1225
        } else if (adjuster instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1226
            return resolveLocal((LocalDateTime) adjuster);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1227
        } else if (adjuster instanceof OffsetDateTime) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1228
            OffsetDateTime odt = (OffsetDateTime) adjuster;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1229
            return ofLocal(odt.toLocalDateTime(), zone, odt.getOffset());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1230
        } else if (adjuster instanceof Instant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1231
            Instant instant = (Instant) adjuster;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1232
            return create(instant.getEpochSecond(), instant.getNano(), zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1233
        } else if (adjuster instanceof ZoneOffset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1234
            return resolveOffset((ZoneOffset) adjuster);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1235
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1236
        return (ZonedDateTime) adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1237
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1238
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1239
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1240
     * Returns a copy of this date-time with the specified field set to a new value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1241
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1242
     * This returns a {@code ZonedDateTime}, based on this one, with the value
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
     * for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
     * This can be used to change any supported field, such as the year, month or day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1245
     * If it is not possible to set the value, because the field is not supported or for
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1246
     * some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1247
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1248
     * In some cases, changing the specified field can cause the resulting date-time to become invalid,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1249
     * such as changing the month from 31st January to February would make the day-of-month invalid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1250
     * In cases like this, the field is responsible for resolving the date. Typically it will choose
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1251
     * the previous valid date, which would be the last valid day of February in this example.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1252
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1253
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1254
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1255
     * The {@code INSTANT_SECONDS} field will return a date-time with the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1256
     * The zone and nano-of-second are unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
     * The result will have an offset derived from the new instant and original zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
     * If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1259
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1260
     * The {@code OFFSET_SECONDS} field will typically be ignored.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1261
     * The offset of a {@code ZonedDateTime} is controlled primarily by the time-zone.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1262
     * As such, changing the offset does not generally make sense, because there is only
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1263
     * one valid offset for the local date-time and zone.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1264
     * If the zoned date-time is in a daylight savings overlap, then the offset is used
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1265
     * to switch between the two valid offsets. In all other cases, the offset is ignored.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1266
     * If the new offset value is outside the valid range then a {@code DateTimeException} will be thrown.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1267
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1268
     * The other {@link #isSupported(TemporalField) supported fields} will behave as per
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1269
     * the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1270
     * The zone is not part of the calculation and will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1271
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1272
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1273
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1274
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1275
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1276
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1277
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1278
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1279
     * passing {@code this} as the argument. In this case, the field determines
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1280
     * whether and how to adjust the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1281
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1282
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1283
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1284
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1285
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1286
     * @return a {@code ZonedDateTime} based on {@code this} with the specified field set, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1287
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1288
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1289
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1290
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1291
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1292
    public ZonedDateTime with(TemporalField field, long newValue) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1293
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1294
            ChronoField f = (ChronoField) field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1295
            switch (f) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1296
                case INSTANT_SECONDS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1297
                    return create(newValue, getNano(), zone);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1298
                case OFFSET_SECONDS:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1299
                    ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1300
                    return resolveOffset(offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1301
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1302
            return resolveLocal(dateTime.with(field, newValue));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1303
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1304
        return field.adjustInto(this, newValue);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1305
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1306
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1307
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1308
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1309
     * Returns a copy of this {@code ZonedDateTime} with the year value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1310
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1311
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1312
     * {@link LocalDateTime#withYear(int) changing the year} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1313
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1314
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1315
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1316
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1317
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1318
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1319
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1320
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1321
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1322
     * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1323
     * @return a {@code ZonedDateTime} based on this date-time with the requested year, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1324
     * @throws DateTimeException if the year value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1325
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1326
    public ZonedDateTime withYear(int year) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1327
        return resolveLocal(dateTime.withYear(year));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1328
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1329
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1330
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1331
     * Returns a copy of this {@code ZonedDateTime} with the month-of-year value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1332
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1333
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1334
     * {@link LocalDateTime#withMonth(int) changing the month} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1335
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1336
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1337
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1338
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1339
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1340
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1341
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1342
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1343
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1344
     * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1345
     * @return a {@code ZonedDateTime} based on this date-time with the requested month, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1346
     * @throws DateTimeException if the month-of-year value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1347
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1348
    public ZonedDateTime withMonth(int month) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1349
        return resolveLocal(dateTime.withMonth(month));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1350
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1351
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1352
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1353
     * Returns a copy of this {@code ZonedDateTime} with the day-of-month value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1354
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1355
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1356
     * {@link LocalDateTime#withDayOfMonth(int) changing the day-of-month} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1357
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1358
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1359
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1360
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1361
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1362
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1363
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1364
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1365
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1366
     * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1367
     * @return a {@code ZonedDateTime} based on this date-time with the requested day, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1368
     * @throws DateTimeException if the day-of-month value is invalid,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1369
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1370
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1371
    public ZonedDateTime withDayOfMonth(int dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1372
        return resolveLocal(dateTime.withDayOfMonth(dayOfMonth));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1373
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1374
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1375
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1376
     * Returns a copy of this {@code ZonedDateTime} with the day-of-year altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1377
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1378
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1379
     * {@link LocalDateTime#withDayOfYear(int) changing the day-of-year} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1380
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1381
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1382
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1383
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1384
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1385
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1386
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1387
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1388
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1389
     * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1390
     * @return a {@code ZonedDateTime} based on this date with the requested day, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1391
     * @throws DateTimeException if the day-of-year value is invalid,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1392
     *  or if the day-of-year is invalid for the year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1393
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1394
    public ZonedDateTime withDayOfYear(int dayOfYear) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1395
        return resolveLocal(dateTime.withDayOfYear(dayOfYear));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1396
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1397
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1398
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1399
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1400
     * Returns a copy of this {@code ZonedDateTime} with the hour-of-day value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1401
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1402
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1403
     * {@linkplain LocalDateTime#withHour(int) changing the time} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1404
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1405
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1406
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1407
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1408
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1409
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1410
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1411
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1412
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1413
     * @param hour  the hour-of-day to set in the result, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1414
     * @return a {@code ZonedDateTime} based on this date-time with the requested hour, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1415
     * @throws DateTimeException if the hour value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1416
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1417
    public ZonedDateTime withHour(int hour) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1418
        return resolveLocal(dateTime.withHour(hour));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1419
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1420
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1421
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1422
     * Returns a copy of this {@code ZonedDateTime} with the minute-of-hour value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1423
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1424
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1425
     * {@linkplain LocalDateTime#withMinute(int) changing the time} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1426
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1427
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1428
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1429
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1430
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1431
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1432
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1433
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1434
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1435
     * @param minute  the minute-of-hour to set in the result, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1436
     * @return a {@code ZonedDateTime} based on this date-time with the requested minute, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1437
     * @throws DateTimeException if the minute value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1438
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1439
    public ZonedDateTime withMinute(int minute) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1440
        return resolveLocal(dateTime.withMinute(minute));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1441
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1442
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1443
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1444
     * Returns a copy of this {@code ZonedDateTime} with the second-of-minute value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1445
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1446
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1447
     * {@linkplain LocalDateTime#withSecond(int) changing the time} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1448
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1449
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1450
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1451
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1452
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1453
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1454
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1455
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1456
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1457
     * @param second  the second-of-minute to set in the result, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1458
     * @return a {@code ZonedDateTime} based on this date-time with the requested second, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1459
     * @throws DateTimeException if the second value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1460
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1461
    public ZonedDateTime withSecond(int second) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1462
        return resolveLocal(dateTime.withSecond(second));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1463
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1464
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1465
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1466
     * Returns a copy of this {@code ZonedDateTime} with the nano-of-second value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1467
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1468
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1469
     * {@linkplain LocalDateTime#withNano(int) changing the time} of the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1470
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1471
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1472
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1473
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1474
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1475
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1476
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1477
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1478
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1479
     * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1480
     * @return a {@code ZonedDateTime} based on this date-time with the requested nanosecond, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1481
     * @throws DateTimeException if the nano value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1482
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1483
    public ZonedDateTime withNano(int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1484
        return resolveLocal(dateTime.withNano(nanoOfSecond));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1485
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1486
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1487
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1488
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1489
     * Returns a copy of this {@code ZonedDateTime} with the time truncated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1490
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1491
     * Truncation returns a copy of the original date-time with fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1492
     * smaller than the specified unit set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1493
     * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1494
     * will set the second-of-minute and nano-of-second field to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1495
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1496
     * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1497
     * that divides into the length of a standard day without remainder.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1498
     * This includes all supplied time units on {@link ChronoUnit} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1499
     * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1500
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1501
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1502
     * {@link LocalDateTime#truncatedTo(java.time.temporal.TemporalUnit) truncating}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1503
     * the underlying local date-time. This is then converted back to a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1504
     * {@code ZonedDateTime}, using the zone ID to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1505
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1506
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1507
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1508
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1509
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1510
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1511
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1512
     * @param unit  the unit to truncate to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1513
     * @return a {@code ZonedDateTime} based on this date-time with the time truncated, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1514
     * @throws DateTimeException if unable to truncate
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1515
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1516
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1517
    public ZonedDateTime truncatedTo(TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1518
        return resolveLocal(dateTime.truncatedTo(unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1519
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1520
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1521
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1522
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1523
     * Returns a copy of this date-time with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1524
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1525
     * This returns a {@code ZonedDateTime}, based on this one, with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1526
     * The amount is typically {@link Period} or {@link Duration} but may be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1527
     * any other type implementing the {@link TemporalAmount} interface.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1528
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1529
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1530
     * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1531
     * to implement the addition in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1532
     * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1533
     * of the amount implementation to determine if it can be successfully added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1534
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1535
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1536
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1537
     * @param amountToAdd  the amount to add, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1538
     * @return a {@code ZonedDateTime} based on this date-time with the addition made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1539
     * @throws DateTimeException if the addition cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1540
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1541
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1542
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1543
    public ZonedDateTime plus(TemporalAmount amountToAdd) {
20517
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1544
        if (amountToAdd instanceof Period) {
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1545
            Period periodToAdd = (Period) amountToAdd;
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1546
            return resolveLocal(dateTime.plus(periodToAdd));
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1547
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1548
        Objects.requireNonNull(amountToAdd, "amountToAdd");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1549
        return (ZonedDateTime) amountToAdd.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1550
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1551
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1552
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1553
     * Returns a copy of this date-time with the specified amount added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1554
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1555
     * This returns a {@code ZonedDateTime}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1556
     * in terms of the unit added. If it is not possible to add the amount, because the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1557
     * unit is not supported or for some other reason, an exception is thrown.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1558
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1559
     * If the field is a {@link ChronoUnit} then the addition is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1560
     * The zone is not part of the calculation and will be unchanged in the result.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1561
     * The calculation for date and time units differ.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1562
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1563
     * Date units operate on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1564
     * The period is first added to the local date-time, then converted back
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1565
     * to a zoned date-time using the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1566
     * The conversion uses {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1567
     * with the offset before the addition.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1568
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1569
     * Time units operate on the instant time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1570
     * The period is first added to the local date-time, then converted back to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1571
     * a zoned date-time using the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1572
     * The conversion uses {@link #ofInstant(LocalDateTime, ZoneOffset, ZoneId)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1573
     * with the offset before the addition.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1574
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1575
     * If the field is not a {@code ChronoUnit}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1576
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1577
     * passing {@code this} as the argument. In this case, the unit determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1578
     * whether and how to perform the addition.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1579
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1580
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1581
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1582
     * @param amountToAdd  the amount of the unit to add to the result, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1583
     * @param unit  the unit of the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1584
     * @return a {@code ZonedDateTime} based on this date-time with the specified amount added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1585
     * @throws DateTimeException if the addition cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1586
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1587
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1588
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1589
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1590
    public ZonedDateTime plus(long amountToAdd, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1591
        if (unit instanceof ChronoUnit) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1592
            if (unit.isDateBased()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1593
                return resolveLocal(dateTime.plus(amountToAdd, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1594
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1595
                return resolveInstant(dateTime.plus(amountToAdd, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1596
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1597
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1598
        return unit.addTo(this, amountToAdd);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1599
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1600
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1601
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1602
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1603
     * Returns a copy of this {@code ZonedDateTime} with the specified period in years added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1604
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1605
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1606
     * {@link LocalDateTime#plusYears(long) adding years} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1607
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1608
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1609
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1610
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1611
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1612
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1613
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1614
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1615
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1616
     * @param years  the years to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1617
     * @return a {@code ZonedDateTime} based on this date-time with the years added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1618
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1619
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1620
    public ZonedDateTime plusYears(long years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1621
        return resolveLocal(dateTime.plusYears(years));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1622
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1623
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1624
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1625
     * Returns a copy of this {@code ZonedDateTime} with the specified period in months added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1626
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1627
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1628
     * {@link LocalDateTime#plusMonths(long) adding months} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1629
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1630
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1631
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1632
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1633
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1634
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1635
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1636
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1637
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1638
     * @param months  the months to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1639
     * @return a {@code ZonedDateTime} based on this date-time with the months added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1640
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1641
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1642
    public ZonedDateTime plusMonths(long months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1643
        return resolveLocal(dateTime.plusMonths(months));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1644
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1645
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1646
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1647
     * Returns a copy of this {@code ZonedDateTime} with the specified period in weeks added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1648
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1649
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1650
     * {@link LocalDateTime#plusWeeks(long) adding weeks} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1651
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1652
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1653
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1654
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1655
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1656
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1657
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1658
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1659
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1660
     * @param weeks  the weeks to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1661
     * @return a {@code ZonedDateTime} based on this date-time with the weeks added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1662
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1663
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1664
    public ZonedDateTime plusWeeks(long weeks) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1665
        return resolveLocal(dateTime.plusWeeks(weeks));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1666
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1667
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1668
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1669
     * Returns a copy of this {@code ZonedDateTime} with the specified period in days added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1670
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1671
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1672
     * {@link LocalDateTime#plusDays(long) adding days} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1673
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1674
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1675
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1676
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1677
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1678
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1679
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1680
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1681
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1682
     * @param days  the days to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1683
     * @return a {@code ZonedDateTime} based on this date-time with the days added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1684
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1685
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1686
    public ZonedDateTime plusDays(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1687
        return resolveLocal(dateTime.plusDays(days));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1688
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1689
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1690
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1691
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1692
     * Returns a copy of this {@code ZonedDateTime} with the specified period in hours added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1693
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1694
     * This operates on the instant time-line, such that adding one hour will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1695
     * always be a duration of one hour later.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1696
     * This may cause the local date-time to change by an amount other than one hour.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1697
     * Note that this is a different approach to that used by days, months and years,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1698
     * thus adding one day is not the same as adding 24 hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1699
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1700
     * For example, consider a time-zone where the spring DST cutover means that the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1701
     * local times 01:00 to 01:59 occur twice changing from offset +02:00 to +01:00.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20747
diff changeset
  1702
     * <ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1703
     * <li>Adding one hour to 00:30+02:00 will result in 01:30+02:00
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1704
     * <li>Adding one hour to 01:30+02:00 will result in 01:30+01:00
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1705
     * <li>Adding one hour to 01:30+01:00 will result in 02:30+01:00
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1706
     * <li>Adding three hours to 00:30+02:00 will result in 02:30+01:00
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20747
diff changeset
  1707
     * </ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1708
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1709
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1710
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1711
     * @param hours  the hours to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1712
     * @return a {@code ZonedDateTime} based on this date-time with the hours added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1713
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1714
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1715
    public ZonedDateTime plusHours(long hours) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1716
        return resolveInstant(dateTime.plusHours(hours));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1717
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1718
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1719
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1720
     * Returns a copy of this {@code ZonedDateTime} with the specified period in minutes added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1721
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1722
     * This operates on the instant time-line, such that adding one minute will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1723
     * always be a duration of one minute later.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1724
     * This may cause the local date-time to change by an amount other than one minute.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1725
     * Note that this is a different approach to that used by days, months and years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1726
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1727
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1728
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1729
     * @param minutes  the minutes to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1730
     * @return a {@code ZonedDateTime} based on this date-time with the minutes added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1731
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1732
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1733
    public ZonedDateTime plusMinutes(long minutes) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1734
        return resolveInstant(dateTime.plusMinutes(minutes));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1735
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1736
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1737
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1738
     * Returns a copy of this {@code ZonedDateTime} with the specified period in seconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1739
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1740
     * This operates on the instant time-line, such that adding one second will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1741
     * always be a duration of one second later.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1742
     * This may cause the local date-time to change by an amount other than one second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1743
     * Note that this is a different approach to that used by days, months and years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1744
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1745
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1746
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1747
     * @param seconds  the seconds to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1748
     * @return a {@code ZonedDateTime} based on this date-time with the seconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1749
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1750
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1751
    public ZonedDateTime plusSeconds(long seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1752
        return resolveInstant(dateTime.plusSeconds(seconds));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1753
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1754
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1755
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1756
     * Returns a copy of this {@code ZonedDateTime} with the specified period in nanoseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1757
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1758
     * This operates on the instant time-line, such that adding one nano will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1759
     * always be a duration of one nano later.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1760
     * This may cause the local date-time to change by an amount other than one nano.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1761
     * Note that this is a different approach to that used by days, months and years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1762
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1763
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1764
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1765
     * @param nanos  the nanos to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1766
     * @return a {@code ZonedDateTime} based on this date-time with the nanoseconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1767
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1768
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1769
    public ZonedDateTime plusNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1770
        return resolveInstant(dateTime.plusNanos(nanos));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1771
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1772
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1773
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1774
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1775
     * Returns a copy of this date-time with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1776
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1777
     * This returns a {@code ZonedDateTime}, based on this one, with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1778
     * The amount is typically {@link Period} or {@link Duration} but may be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1779
     * any other type implementing the {@link TemporalAmount} interface.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1780
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1781
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1782
     * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1783
     * to implement the subtraction in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1784
     * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1785
     * of the amount implementation to determine if it can be successfully subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1786
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1787
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1788
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1789
     * @param amountToSubtract  the amount to subtract, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1790
     * @return a {@code ZonedDateTime} based on this date-time with the subtraction made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1791
     * @throws DateTimeException if the subtraction cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1792
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1793
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1794
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1795
    public ZonedDateTime minus(TemporalAmount amountToSubtract) {
20517
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1796
        if (amountToSubtract instanceof Period) {
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1797
            Period periodToSubtract = (Period) amountToSubtract;
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1798
            return resolveLocal(dateTime.minus(periodToSubtract));
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1799
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1800
        Objects.requireNonNull(amountToSubtract, "amountToSubtract");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1801
        return (ZonedDateTime) amountToSubtract.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1802
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1803
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1804
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1805
     * Returns a copy of this date-time with the specified amount subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1806
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1807
     * This returns a {@code ZonedDateTime}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1808
     * in terms of the unit subtracted. If it is not possible to subtract the amount,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1809
     * because the unit is not supported or for some other reason, an exception is thrown.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1810
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1811
     * The calculation for date and time units differ.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1812
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1813
     * Date units operate on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1814
     * The period is first subtracted from the local date-time, then converted back
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1815
     * to a zoned date-time using the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1816
     * The conversion uses {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1817
     * with the offset before the subtraction.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1818
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1819
     * Time units operate on the instant time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1820
     * The period is first subtracted from the local date-time, then converted back to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1821
     * a zoned date-time using the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1822
     * The conversion uses {@link #ofInstant(LocalDateTime, ZoneOffset, ZoneId)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1823
     * with the offset before the subtraction.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1824
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1825
     * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1826
     * See that method for a full description of how addition, and thus subtraction, works.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1827
     * <p>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1828
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1829
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1830
     * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1831
     * @param unit  the unit of the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1832
     * @return a {@code ZonedDateTime} based on this date-time with the specified amount subtracted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1833
     * @throws DateTimeException if the subtraction cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1834
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1835
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1836
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1837
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1838
    public ZonedDateTime minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1839
        return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1840
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1841
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1842
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1843
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1844
     * Returns a copy of this {@code ZonedDateTime} with the specified period in years subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1845
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1846
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1847
     * {@link LocalDateTime#minusYears(long) subtracting years} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1848
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1849
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1850
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1851
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1852
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1853
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1854
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1855
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1856
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1857
     * @param years  the years to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1858
     * @return a {@code ZonedDateTime} based on this date-time with the years subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1859
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1860
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1861
    public ZonedDateTime minusYears(long years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1862
        return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1863
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1864
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1865
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1866
     * Returns a copy of this {@code ZonedDateTime} with the specified period in months subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1867
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1868
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1869
     * {@link LocalDateTime#minusMonths(long) subtracting months} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1870
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1871
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1872
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1873
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1874
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1875
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1876
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1877
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1878
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1879
     * @param months  the months to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1880
     * @return a {@code ZonedDateTime} based on this date-time with the months subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1881
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1882
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1883
    public ZonedDateTime minusMonths(long months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1884
        return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1885
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1886
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1887
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1888
     * Returns a copy of this {@code ZonedDateTime} with the specified period in weeks subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1889
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1890
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1891
     * {@link LocalDateTime#minusWeeks(long) subtracting weeks} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1892
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1893
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1894
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1895
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1896
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1897
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1898
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1899
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1900
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1901
     * @param weeks  the weeks to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1902
     * @return a {@code ZonedDateTime} based on this date-time with the weeks subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1903
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1904
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1905
    public ZonedDateTime minusWeeks(long weeks) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1906
        return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1907
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1908
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1909
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1910
     * Returns a copy of this {@code ZonedDateTime} with the specified period in days subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1911
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1912
     * This operates on the local time-line,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1913
     * {@link LocalDateTime#minusDays(long) subtracting days} to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1914
     * This is then converted back to a {@code ZonedDateTime}, using the zone ID
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1915
     * to obtain the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1916
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1917
     * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1918
     * then the offset will be retained if possible, otherwise the earlier offset will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1919
     * If in a gap, the local date-time will be adjusted forward by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1920
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1921
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1922
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1923
     * @param days  the days to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1924
     * @return a {@code ZonedDateTime} based on this date-time with the days subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1925
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1926
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1927
    public ZonedDateTime minusDays(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1928
        return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1929
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1930
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1931
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1932
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1933
     * Returns a copy of this {@code ZonedDateTime} with the specified period in hours subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1934
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1935
     * This operates on the instant time-line, such that subtracting one hour will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1936
     * always be a duration of one hour earlier.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1937
     * This may cause the local date-time to change by an amount other than one hour.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1938
     * Note that this is a different approach to that used by days, months and years,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1939
     * thus subtracting one day is not the same as adding 24 hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1940
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1941
     * For example, consider a time-zone where the spring DST cutover means that the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1942
     * local times 01:00 to 01:59 occur twice changing from offset +02:00 to +01:00.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20747
diff changeset
  1943
     * <ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1944
     * <li>Subtracting one hour from 02:30+01:00 will result in 01:30+02:00
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1945
     * <li>Subtracting one hour from 01:30+01:00 will result in 01:30+02:00
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1946
     * <li>Subtracting one hour from 01:30+02:00 will result in 00:30+01:00
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1947
     * <li>Subtracting three hours from 02:30+01:00 will result in 00:30+02:00
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20747
diff changeset
  1948
     * </ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1949
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1950
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1951
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1952
     * @param hours  the hours to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1953
     * @return a {@code ZonedDateTime} based on this date-time with the hours subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1954
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1955
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1956
    public ZonedDateTime minusHours(long hours) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1957
        return (hours == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1) : plusHours(-hours));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1958
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1959
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1960
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1961
     * Returns a copy of this {@code ZonedDateTime} with the specified period in minutes subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1962
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1963
     * This operates on the instant time-line, such that subtracting one minute will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1964
     * always be a duration of one minute earlier.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1965
     * This may cause the local date-time to change by an amount other than one minute.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1966
     * Note that this is a different approach to that used by days, months and years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1967
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1968
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1969
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1970
     * @param minutes  the minutes to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1971
     * @return a {@code ZonedDateTime} based on this date-time with the minutes subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1972
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1973
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1974
    public ZonedDateTime minusMinutes(long minutes) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1975
        return (minutes == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1) : plusMinutes(-minutes));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1976
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1977
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1978
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1979
     * Returns a copy of this {@code ZonedDateTime} with the specified period in seconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1980
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1981
     * This operates on the instant time-line, such that subtracting one second will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1982
     * always be a duration of one second earlier.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1983
     * This may cause the local date-time to change by an amount other than one second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1984
     * Note that this is a different approach to that used by days, months and years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1985
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1986
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1987
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1988
     * @param seconds  the seconds to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1989
     * @return a {@code ZonedDateTime} based on this date-time with the seconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1990
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1991
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1992
    public ZonedDateTime minusSeconds(long seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1993
        return (seconds == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1) : plusSeconds(-seconds));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1994
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1995
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1996
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1997
     * Returns a copy of this {@code ZonedDateTime} with the specified period in nanoseconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1998
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1999
     * This operates on the instant time-line, such that subtracting one nano will
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2000
     * always be a duration of one nano earlier.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2001
     * This may cause the local date-time to change by an amount other than one nano.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2002
     * Note that this is a different approach to that used by days, months and years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2003
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2004
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2005
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2006
     * @param nanos  the nanos to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2007
     * @return a {@code ZonedDateTime} based on this date-time with the nanoseconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2008
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2009
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2010
    public ZonedDateTime minusNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2011
        return (nanos == Long.MIN_VALUE ? plusNanos(Long.MAX_VALUE).plusNanos(1) : plusNanos(-nanos));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2012
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2013
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2014
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2015
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2016
     * Queries this date-time using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2017
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2018
     * This queries this date-time using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2019
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2020
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2021
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2022
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2023
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2024
     * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2025
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2026
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2027
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2028
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2029
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2030
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2031
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2032
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2033
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2034
    public <R> R query(TemporalQuery<R> query) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2035
        return ChronoZonedDateTime.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2036
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2037
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2038
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2039
     * Calculates the amount of time until another date-time in terms of the specified unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2040
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2041
     * This calculates the amount of time between two {@code ZonedDateTime}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2042
     * objects in terms of a single {@code TemporalUnit}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2043
     * The start and end points are {@code this} and the specified date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2044
     * The result will be negative if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2045
     * For example, the period in days between two date-times can be calculated
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2046
     * using {@code startDateTime.until(endDateTime, DAYS)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2047
     * <p>
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2048
     * The {@code Temporal} passed to this method is converted to a
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2049
     * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2050
     * If the time-zone differs between the two zoned date-times, the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2051
     * end date-time is normalized to have the same zone as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2052
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2053
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2054
     * complete units between the two date-times.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2055
     * For example, the period in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2056
     * will only be one month as it is one minute short of two months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2057
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2058
     * There are two equivalent ways of using this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2059
     * The first is to invoke this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2060
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2061
     * <pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2062
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2063
     *   amount = start.until(end, MONTHS);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2064
     *   amount = MONTHS.between(start, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2065
     * </pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2066
     * The choice should be made based on which makes the code more readable.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2067
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2068
     * The calculation is implemented in this method for {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2069
     * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2070
     * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2071
     * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2072
     * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2073
     * Other {@code ChronoUnit} values will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2074
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2075
     * The calculation for date and time units differ.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2076
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2077
     * Date units operate on the local time-line, using the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2078
     * For example, the period from noon on day 1 to noon the following day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2079
     * in days will always be counted as exactly one day, irrespective of whether
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2080
     * there was a daylight savings change or not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2081
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2082
     * Time units operate on the instant time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2083
     * The calculation effectively converts both zoned date-times to instants
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2084
     * and then calculates the period between the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2085
     * For example, the period from noon on day 1 to noon the following day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2086
     * in hours may be 23, 24 or 25 hours (or some other amount) depending on
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2087
     * whether there was a daylight savings change or not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2088
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2089
     * If the unit is not a {@code ChronoUnit}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2090
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2091
     * passing {@code this} as the first argument and the converted input temporal
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2092
     * as the second argument.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2093
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2094
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2095
     *
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2096
     * @param endExclusive  the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2097
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2098
     * @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: 20517
diff changeset
  2099
     * @throws DateTimeException if the amount cannot be calculated, or the end
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2100
     *  temporal cannot be converted to a {@code ZonedDateTime}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2101
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2102
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2103
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2104
    @Override
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2105
    public long until(Temporal endExclusive, TemporalUnit unit) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2106
        ZonedDateTime end = ZonedDateTime.from(endExclusive);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2107
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2108
            end = end.withZoneSameInstant(zone);
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2109
            if (unit.isDateBased()) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2110
                return dateTime.until(end.dateTime, unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2111
            } else {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2112
                return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2113
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2114
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  2115
        return unit.between(this, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2116
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2117
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2118
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2119
     * Formats this date-time using the specified formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2120
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2121
     * 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
  2122
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2123
     * @param formatter  the formatter to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2124
     * @return the formatted date-time string, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2125
     * @throws DateTimeException if an error occurs during printing
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2126
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2127
    @Override  // override for Javadoc and performance
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2128
    public String format(DateTimeFormatter formatter) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2129
        Objects.requireNonNull(formatter, "formatter");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2130
        return formatter.format(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2131
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  2132
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2133
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2134
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2135
     * Converts this date-time to an {@code OffsetDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2136
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2137
     * This creates an offset date-time using the local date-time and offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2138
     * The zone ID is ignored.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2139
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2140
     * @return an offset date-time representing the same local date-time and offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2141
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2142
    public OffsetDateTime toOffsetDateTime() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2143
        return OffsetDateTime.of(dateTime, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2144
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2145
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2146
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2147
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2148
     * Checks if this date-time is equal to another date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2149
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2150
     * The comparison is based on the offset date-time and the zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2151
     * Only objects of type {@code ZonedDateTime} are compared, other types return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2152
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2153
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2154
     * @return true if this is equal to the other date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2155
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2156
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2157
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2158
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2159
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2160
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2161
        if (obj instanceof ZonedDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2162
            ZonedDateTime other = (ZonedDateTime) obj;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2163
            return dateTime.equals(other.dateTime) &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2164
                offset.equals(other.offset) &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2165
                zone.equals(other.zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2166
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2167
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2168
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2169
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2170
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2171
     * A hash code for this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2172
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2173
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2174
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2175
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2176
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2177
        return dateTime.hashCode() ^ offset.hashCode() ^ Integer.rotateLeft(zone.hashCode(), 3);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2178
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2179
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2180
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2181
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2182
     * Outputs this date-time as a {@code String}, such as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2183
     * {@code 2007-12-03T10:15:30+01:00[Europe/Paris]}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2184
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2185
     * The format consists of the {@code LocalDateTime} followed by the {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2186
     * If the {@code ZoneId} is not the same as the offset, then the ID is output.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2187
     * The output is compatible with ISO-8601 if the offset and ID are the same.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2188
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2189
     * @return a string representation of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2190
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2191
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2192
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2193
        String str = dateTime.toString() + offset.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2194
        if (offset != zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2195
            str += '[' + zone.toString() + ']';
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2196
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2197
        return str;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2198
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2199
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2200
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2201
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2202
     * Writes the object using a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2203
     * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  2204
     * @serialData
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2205
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  2206
     *  out.writeByte(6);  // identifies a ZonedDateTime
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  2207
     *  // the <a href="../../serialized-form.html#java.time.LocalDateTime">dateTime</a> excluding the one byte header
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2208
     *  // the <a href="../../serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2209
     *  // the <a href="../../serialized-form.html#java.time.ZoneId">zone ID</a> excluding the one byte header
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2210
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2211
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2212
     * @return the instance of {@code Ser}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2213
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2214
    private Object writeReplace() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2215
        return new Ser(Ser.ZONE_DATE_TIME_TYPE, this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2216
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2217
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2218
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2219
     * Defend against malicious streams.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2220
     * @return never
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2221
     * @throws InvalidObjectException always
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2222
     */
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  2223
    private Object readResolve() throws InvalidObjectException {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2224
        throw new InvalidObjectException("Deserialization via serialization delegate");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2225
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2226
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2227
    void writeExternal(DataOutput out) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2228
        dateTime.writeExternal(out);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2229
        offset.writeExternal(out);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2230
        zone.write(out);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2231
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2232
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2233
    static ZonedDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2234
        LocalDateTime dateTime = LocalDateTime.readExternal(in);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2235
        ZoneOffset offset = ZoneOffset.readExternal(in);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2236
        ZoneId zone = (ZoneId) Ser.read(in);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2237
        return ZonedDateTime.ofLenient(dateTime, offset, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2238
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2239
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2240
}