jdk/src/share/classes/java/time/temporal/OffsetTime.java
author dsamersoff
Tue, 12 Feb 2013 16:02:14 +0400
changeset 15657 c588664d547e
parent 15289 3ac550392e43
permissions -rw-r--r--
8007786: JDK-8002048 testcase doesn't work on Solaris Summary: test built in into Solaris shell doesn't have -e operator Reviewed-by: sla, sspitsyn
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.temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.NANO_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoLocalDateTimeImpl.NANOS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoLocalDateTimeImpl.NANOS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.temporal.ChronoLocalDateTimeImpl.NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.temporal.ChronoLocalDateTimeImpl.SECONDS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.temporal.ChronoUnit.NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import java.io.IOException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.io.InvalidObjectException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.io.ObjectInput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.io.ObjectOutput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.io.ObjectStreamException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.io.Serializable;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.time.Clock;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.time.DateTimeException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.Instant;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.LocalDate;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.time.LocalTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.ZoneId;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.ZoneOffset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.format.DateTimeFormatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.format.DateTimeFormatters;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.time.zone.ZoneRules;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 * A time with an offset from UTC/Greenwich in the ISO-8601 calendar system,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * such as {@code 10:15:30+01:00}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * {@code OffsetTime} is an immutable date-time object that represents a time, often
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * viewed as hour-minute-second-offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * This class stores all time fields, to a precision of nanoseconds,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * as well as a zone offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * For example, the value "13:45.30.123456789+02:00" can be stored
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * in an {@code OffsetTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
public final class OffsetTime
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
        implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
     * The minimum supported {@code OffsetTime}, '00:00:00+18:00'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
     * This is the time of midnight at the start of the day in the maximum offset
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
     * (larger offsets are earlier on the time-line).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
     * This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
     * This could be used by an application as a "far past" date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
    public static final OffsetTime MIN = LocalTime.MIN.atOffset(ZoneOffset.MAX);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     * The maximum supported {@code OffsetTime}, '23:59:59.999999999-18:00'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     * This is the time just before midnight at the end of the day in the minimum offset
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     * (larger negative offsets are later on the time-line).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * This combines {@link LocalTime#MAX} and {@link ZoneOffset#MIN}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * This could be used by an application as a "far future" date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
    public static final OffsetTime MAX = LocalTime.MAX.atOffset(ZoneOffset.MIN);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
    private static final long serialVersionUID = 7264499704384272492L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * The local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
    private final LocalTime time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * The offset from UTC/Greenwich.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
    private final ZoneOffset offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     * Obtains the current time from the system clock in the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
     * This will query the {@link java.time.Clock#systemDefaultZone() system clock} in the default
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * time-zone to obtain the current time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * The offset will be calculated from the time-zone in the clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
     * 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
   150
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
     * @return the current time using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
    public static OffsetTime now() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
        return now(Clock.systemDefaultZone());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     * Obtains the current time from the system clock in the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * Specifying the time-zone avoids dependence on the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * The offset will be calculated from the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * 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
   166
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     * @param zone  the zone ID to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     * @return the current time using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    public static OffsetTime now(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
        return now(Clock.system(zone));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * Obtains the current time from the specified clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * This will query the specified clock to obtain the current time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * The offset will be calculated from the time-zone in the clock.
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
     * Using this method allows the use of an alternate clock for testing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     * The alternate clock may be introduced using {@link Clock dependency injection}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * @param clock  the clock to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * @return the current time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    public static OffsetTime now(Clock clock) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
        Objects.requireNonNull(clock, "clock");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
        final Instant now = clock.instant();  // called once
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
        return ofInstant(now, clock.getZone().getRules().getOffset(now));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
    //-----------------------------------------------------------------------
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 an instance of {@code OffsetTime} from a local time and an offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * @param time  the local time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * @param offset  the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     * @return the offset time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    public static OffsetTime of(LocalTime time, ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
        return new OffsetTime(time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * This creates an offset time with the same instant as that specified.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * Finding the offset from UTC/Greenwich is simple as there is only one valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * offset for each instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * The date component of the instant is dropped during the conversion.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * This means that the conversion can never fail due to the instant being
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * out of the valid range of dates.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * @param instant  the instant to create the time from, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @param zone  the time-zone, which may be an offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * @return the offset time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
    public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        Objects.requireNonNull(instant, "instant");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
        ZoneRules rules = zone.getRules();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
        ZoneOffset offset = rules.getOffset(instant);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
        long secsOfDay = instant.getEpochSecond() % SECONDS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
        secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
        if (secsOfDay < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
            secsOfDay += SECONDS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
        LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, instant.getNano());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
        return new OffsetTime(time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
     * Obtains an instance of {@code OffsetTime} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * A {@code TemporalAccessor} represents some form of date and time information.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     * This factory converts the arbitrary temporal object to an instance of {@code OffsetTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * The conversion extracts and combines {@code LocalTime} and {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
     * allowing it to be used in queries via method reference, {@code OffsetTime::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * @return the offset time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     * @throws DateTimeException if unable to convert to an {@code OffsetTime}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
    public static OffsetTime from(TemporalAccessor temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
        if (temporal instanceof OffsetTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
            return (OffsetTime) temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
        try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
            LocalTime time = LocalTime.from(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
            ZoneOffset offset = ZoneOffset.from(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
            return new OffsetTime(time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
        } catch (DateTimeException ex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
            throw new DateTimeException("Unable to obtain OffsetTime from TemporalAccessor: " + temporal.getClass(), ex);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * Obtains an instance of {@code OffsetTime} from a text string such as {@code 10:15:30+01:00}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * The string must represent a valid time and is parsed using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * {@link java.time.format.DateTimeFormatters#isoOffsetTime()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * @param text  the text to parse such as "10:15:30+01:00", not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * @return the parsed local time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
    public static OffsetTime parse(CharSequence text) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
        return parse(text, DateTimeFormatters.isoOffsetTime());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     * Obtains an instance of {@code OffsetTime} from a text string using a specific formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
     * The text is parsed using the formatter, returning a time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     * @param formatter  the formatter to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * @return the parsed offset time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
    public static OffsetTime parse(CharSequence text, DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
        return formatter.parse(text, OffsetTime::from);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * @param time  the local time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     * @param offset  the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
    private OffsetTime(LocalTime time, ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
        this.time = Objects.requireNonNull(time, "time");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
        this.offset = Objects.requireNonNull(offset, "offset");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
     * Returns a new time based on this one, returning {@code this} where possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * @param time  the time to create with, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * @param offset  the zone offset to create with, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
    private OffsetTime with(LocalTime time, ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
        if (this.time == time && this.offset.equals(offset)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
        return new OffsetTime(time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * This checks if this time can be queried for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
     * If false, then calling the {@link #range(TemporalField) range} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * {@link #get(TemporalField) get} methods will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
     * The supported fields are:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
     * <li>{@code NANO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * <li>{@code NANO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * <li>{@code MICRO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
     * <li>{@code MICRO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     * <li>{@code MILLI_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * <li>{@code MILLI_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * <li>{@code SECOND_OF_MINUTE}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * <li>{@code SECOND_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * <li>{@code MINUTE_OF_HOUR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     * <li>{@code MINUTE_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
     * <li>{@code HOUR_OF_AMPM}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * <li>{@code CLOCK_HOUR_OF_AMPM}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     * <li>{@code HOUR_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     * <li>{@code CLOCK_HOUR_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
     * <li>{@code AMPM_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
     * <li>{@code OFFSET_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * If the field is not a {@code ChronoField}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
     * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * @return true if the field is supported on this time, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
            return ((ChronoField) field).isTimeField() || field == OFFSET_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
        return field != null && field.doIsSupported(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
     * This time is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
     * 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
   371
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
     * The {@link #isSupported(TemporalField) supported fields} will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
     * appropriate range instances.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
     * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
     * If the field is not a {@code ChronoField}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
     * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     * @throws DateTimeException if the range for the field cannot be obtained
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
            if (field == OFFSET_SECONDS) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
                return field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
            return time.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
        return field.doRange(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     * Gets the value of the specified field from this time as an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * This queries this time for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     * 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
   403
     * 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
   404
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
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
     * If the field is not a {@code ChronoField}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * @throws DateTimeException if a value for the field cannot be obtained
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
        return Temporal.super.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
     * Gets the value of the specified field from this time as a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     * This queries this time for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     * 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
   432
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * values based on this time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * If the field is not a {@code ChronoField}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
     * @throws DateTimeException if a value for the field cannot be obtained
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
            if (field == OFFSET_SECONDS) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
                return getOffset().getTotalSeconds();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
            return time.getLong(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
        return field.doGet(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
     * Gets the zone offset, such as '+01:00'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
     * This is the offset of the local time from UTC/Greenwich.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
     * @return the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
    public ZoneOffset getOffset() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
        return offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
     * Returns a copy of this {@code OffsetTime} with the specified offset ensuring
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
     * that the result has the same local time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
     * This method returns an object with the same {@code LocalTime} and the specified {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
     * No calculation is needed or performed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
     * For example, if this time represents {@code 10:30+02:00} and the offset specified is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
     * {@code +03:00}, then this method will return {@code 10:30+03:00}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
     * To take into account the difference between the offsets, and adjust the time fields,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
     * use {@link #withOffsetSameInstant}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * @param offset  the zone offset to change to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     * @return an {@code OffsetTime} based on this time with the requested offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
    public OffsetTime withOffsetSameLocal(ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
        return offset != null && offset.equals(this.offset) ? this : new OffsetTime(time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
     * Returns a copy of this {@code OffsetTime} with the specified offset ensuring
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
     * that the result is at the same instant on an implied day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
     * This method returns an object with the specified {@code ZoneOffset} and a {@code LocalTime}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
     * adjusted by the difference between the two offsets.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
     * This will result in the old and new objects representing the same instant an an implied day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * This is useful for finding the local time in a different offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
     * For example, if this time represents {@code 10:30+02:00} and the offset specified is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     * {@code +03:00}, then this method will return {@code 11:30+03:00}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * To change the offset without adjusting the local time use {@link #withOffsetSameLocal}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     * @param offset  the zone offset to change to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     * @return an {@code OffsetTime} based on this time with the requested offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
    public OffsetTime withOffsetSameInstant(ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
        if (offset.equals(this.offset)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
        int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
        LocalTime adjusted = time.plusSeconds(difference);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
        return new OffsetTime(adjusted, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
     * Gets the {@code LocalTime} part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
     * This returns a {@code LocalTime} with the same hour, minute, second and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
     * nanosecond as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
     * @return the time part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
    public LocalTime getTime() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
        return time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
     * Gets the hour-of-day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
     * @return the hour-of-day, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
    public int getHour() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
        return time.getHour();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
     * Gets the minute-of-hour field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
     * @return the minute-of-hour, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
    public int getMinute() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
        return time.getMinute();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
     * Gets the second-of-minute field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
     * @return the second-of-minute, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
    public int getSecond() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
        return time.getSecond();
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
     * Gets the nano-of-second field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   563
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
     * @return the nano-of-second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
    public int getNano() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
        return time.getNano();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   569
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   570
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
     * Returns an adjusted copy of this time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   574
     * This returns a new {@code OffsetTime}, based on this one, with the time adjusted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
     * The adjustment takes place using the specified adjuster strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
     * Read the documentation of the adjuster to understand what adjustment will be made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   578
     * A simple adjuster might simply set the one of the fields, such as the hour field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
     * A more complex adjuster might set the time to the last hour of the day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   581
     * The classes {@link LocalTime} and {@link ZoneOffset} implement {@code TemporalAdjuster},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
     * thus this method can be used to change the time or offset:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
     *  result = offsetTime.with(time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     *  result = offsetTime.with(offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
     * specified adjuster passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   591
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
     * @param adjuster the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
     * @return an {@code OffsetTime} based on {@code this} with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
     * @throws DateTimeException if the adjustment cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
    public OffsetTime with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
        // optimizations
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
        if (adjuster instanceof LocalTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
            return with((LocalTime) adjuster, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
        } else if (adjuster instanceof ZoneOffset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
            return with(time, (ZoneOffset) adjuster);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
        } else if (adjuster instanceof OffsetTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   607
            return (OffsetTime) adjuster;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   608
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
        return (OffsetTime) adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
     * Returns a copy of this time with the specified field set to a new value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   614
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   615
     * This returns a new {@code OffsetTime}, based on this one, with the value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
     * for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
     * This can be used to change any supported field, such as the hour, minute or second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
     * 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
   619
     * some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
     * The {@code OFFSET_SECONDS} field will return a time with the specified offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
     * The local time is unaltered. If the new offset value is outside the valid range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     * then a {@code DateTimeException} will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   627
     * The other {@link #isSupported(TemporalField) supported fields} will behave as per
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
     * the matching method on {@link LocalTime#with(TemporalField, long)} LocalTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
     * In this case, the offset is not part of the calculation and will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
     * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
     * If the field is not a {@code ChronoField}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
     * is obtained by invoking {@code TemporalField.doWith(Temporal, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
     * passing {@code this} as the argument. In this case, the field determines
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
     * whether and how to adjust the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
     * @return an {@code OffsetTime} based on {@code this} with the specified field set, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
     * @throws DateTimeException if the field cannot be set
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
    public OffsetTime with(TemporalField field, long newValue) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
            if (field == OFFSET_SECONDS) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
                ChronoField f = (ChronoField) field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
                return with(time, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
            return with(time.with(field, newValue), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
        return field.doWith(this, newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
     * Returns a copy of this {@code OffsetTime} with the hour-of-day value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
     * The offset does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * @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
   667
     * @return an {@code OffsetTime} based on this time with the requested hour, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     * @throws DateTimeException if the hour value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
    public OffsetTime withHour(int hour) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
        return with(time.withHour(hour), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     * Returns a copy of this {@code OffsetTime} with the minute-of-hour value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
     * The offset does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
     * @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
   682
     * @return an {@code OffsetTime} based on this time with the requested minute, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
     * @throws DateTimeException if the minute value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
    public OffsetTime withMinute(int minute) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
        return with(time.withMinute(minute), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
     * Returns a copy of this {@code OffsetTime} with the second-of-minute value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     * The offset does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
     * @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
   697
     * @return an {@code OffsetTime} based on this time with the requested second, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
     * @throws DateTimeException if the second value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   700
    public OffsetTime withSecond(int second) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
        return with(time.withSecond(second), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
     * Returns a copy of this {@code OffsetTime} with the nano-of-second value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
     * The offset does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
     * @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
   712
     * @return an {@code OffsetTime} based on this time with the requested nanosecond, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
     * @throws DateTimeException if the nanos value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
    public OffsetTime withNano(int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
        return with(time.withNano(nanoOfSecond), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   719
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   720
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
     * Returns a copy of this {@code OffsetTime} with the time truncated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
     * Truncation returns a copy of the original time with fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   724
     * smaller than the specified unit set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   725
     * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   726
     * will set the second-of-minute and nano-of-second field to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   727
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   728
     * Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   729
     * units with an exact duration can be used, other units throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   731
     * The offset does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   732
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   733
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   734
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
     * @param unit  the unit to truncate to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
     * @return an {@code OffsetTime} based on this time with the time truncated, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   737
     * @throws DateTimeException if unable to truncate
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   738
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   739
    public OffsetTime truncatedTo(TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   740
        return with(time.truncatedTo(unit), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   741
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   742
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
     * Returns a copy of this date with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
     * This method returns a new time based on this time with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
     * The adder is typically {@link java.time.Period} but may be any other type implementing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
     * the {@link TemporalAdder} interface.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
     * The calculation is delegated to the specified adjuster, which typically calls
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
     * back to {@link #plus(long, TemporalUnit)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   752
     * The offset is not part of the calculation and will be unchanged in the result.
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
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
     * @param adder  the adder to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
     * @return an {@code OffsetTime} based on this time with the addition made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
     * @throws DateTimeException if the addition cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
    public OffsetTime plus(TemporalAdder adder) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
        return (OffsetTime) adder.addTo(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
     * Returns a copy of this time with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   769
     * This method returns a new time based on this time with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   770
     * This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
     * The unit is responsible for the details of the calculation, including the resolution
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
     * of any edge cases in the calculation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
     * The offset is not part of the calculation and will be unchanged in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * @param amountToAdd  the amount of the unit to add to the result, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
     * @param unit  the unit of the period to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * @return an {@code OffsetTime} based on this time with the specified period added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
     * @throws DateTimeException if the unit cannot be added to this type
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
    public OffsetTime plus(long amountToAdd, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
            return with(time.plus(amountToAdd, unit), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
        return unit.doPlus(this, amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
     * Returns a copy of this {@code OffsetTime} with the specified period in hours added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
     * This adds the specified number of hours to this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   797
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   798
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
     * @param hours  the hours to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   800
     * @return an {@code OffsetTime} based on this time with the hours added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
    public OffsetTime plusHours(long hours) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   803
        return with(time.plusHours(hours), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   804
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   805
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   806
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   807
     * Returns a copy of this {@code OffsetTime} with the specified period in minutes added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   808
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   809
     * This adds the specified number of minutes to this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   810
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
     * @param minutes  the minutes to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
     * @return an {@code OffsetTime} based on this time with the minutes added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
    public OffsetTime plusMinutes(long minutes) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
        return with(time.plusMinutes(minutes), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   819
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
     * Returns a copy of this {@code OffsetTime} with the specified period in seconds added.
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
     * This adds the specified number of seconds to this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   825
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   826
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   827
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   828
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   829
     * @param seconds  the seconds to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   830
     * @return an {@code OffsetTime} based on this time with the seconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   832
    public OffsetTime plusSeconds(long seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
        return with(time.plusSeconds(seconds), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   835
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   836
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
     * Returns a copy of this {@code OffsetTime} with the specified period in nanoseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
     * This adds the specified number of nanoseconds to this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   841
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   842
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
     * @param nanos  the nanos to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
     * @return an {@code OffsetTime} based on this time with the nanoseconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
    public OffsetTime plusNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
        return with(time.plusNanos(nanos), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
    }
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
     * Returns a copy of this time with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
     * This method returns a new time based on this time with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
     * The subtractor is typically {@link java.time.Period} but may be any other type implementing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
     * the {@link TemporalSubtractor} interface.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
     * The calculation is delegated to the specified adjuster, which typically calls
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
     * back to {@link #minus(long, TemporalUnit)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
     * The offset is not part of the calculation and will be unchanged in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
     * @param subtractor  the subtractor to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
     * @return an {@code OffsetTime} based on this time with the subtraction made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
     * @throws DateTimeException if the subtraction cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
    public OffsetTime minus(TemporalSubtractor subtractor) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
        return (OffsetTime) subtractor.subtractFrom(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
     * Returns a copy of this time with the specified period subtracted.
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 method returns a new time based on this time with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
     * This can be used to subtract any period that is defined by a unit, for example to subtract hours, minutes or seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
     * The unit is responsible for the details of the calculation, including the resolution
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
     * of any edge cases in the calculation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
     * The offset is not part of the calculation and will be unchanged in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
     * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
     * @param unit  the unit of the period to subtract, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
     * @return an {@code OffsetTime} based on this time with the specified period subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
     * @throws DateTimeException if the unit cannot be added to this type
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
    public OffsetTime minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
        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
   893
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     * Returns a copy of this {@code OffsetTime} with the specified period in hours subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     * This subtracts the specified number of hours from this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
     * The calculation wraps around midnight.
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
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     * @param hours  the hours to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     * @return an {@code OffsetTime} based on this time with the hours subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
    public OffsetTime minusHours(long hours) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
        return with(time.minusHours(hours), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
     * Returns a copy of this {@code OffsetTime} with the specified period in minutes subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     * This subtracts the specified number of minutes from this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
     * @param minutes  the minutes to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
     * @return an {@code OffsetTime} based on this time with the minutes subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
    public OffsetTime minusMinutes(long minutes) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
        return with(time.minusMinutes(minutes), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
     * Returns a copy of this {@code OffsetTime} with the specified period in seconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
     * This subtracts the specified number of seconds from this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   931
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   932
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   933
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   934
     * @param seconds  the seconds to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   935
     * @return an {@code OffsetTime} based on this time with the seconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   936
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   937
    public OffsetTime minusSeconds(long seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   938
        return with(time.minusSeconds(seconds), offset);
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
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   942
     * Returns a copy of this {@code OffsetTime} with the specified period in nanoseconds subtracted.
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 subtracts the specified number of nanoseconds from this time, returning a new time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   945
     * The calculation wraps around midnight.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   948
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   949
     * @param nanos  the nanos to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   950
     * @return an {@code OffsetTime} based on this time with the nanoseconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   951
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   952
    public OffsetTime minusNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
        return with(time.minusNanos(nanos), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   954
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   955
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   956
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   957
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
     * Queries this time using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   960
     * This queries this time using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   961
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   962
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   963
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   964
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   965
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   966
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   967
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   969
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   970
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   971
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   972
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
    public <R> R query(TemporalQuery<R> query) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
        if (query == Queries.precision()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
            return (R) NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
        } else if (query == Queries.offset() || query == Queries.zone()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
            return (R) getOffset();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
        return Temporal.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
    }
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
     * Adjusts the specified temporal object to have the same offset and time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     * as this object.
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 temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
     * with the offset and time changed to be the same as this.
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
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
     * twice, passing {@link ChronoField#OFFSET_SECONDS} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
     * {@link ChronoField#NANO_OF_DAY} as the fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   998
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
     *   temporal = thisOffsetTime.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
     *   temporal = temporal.with(thisOffsetTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
        return temporal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
                .with(OFFSET_SECONDS, getOffset().getTotalSeconds())
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
                .with(NANO_OF_DAY, time.toNanoOfDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1018
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * Calculates the period between this time and another time in
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1021
     * terms of the specified unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
     * This calculates the period between two times in terms of a single unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
     * The start and end points are {@code this} and the specified time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
     * The result will be negative if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
     * For example, the period in hours between two times can be calculated
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
     * using {@code startTime.periodUntil(endTime, HOURS)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
     * The {@code Temporal} passed to this method must be an {@code OffsetTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
     * If the offset differs between the two times, then the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
     * end time is normalized to have the same offset as this time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
     * complete units between the two times.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
     * For example, the period in hours between 11:30Z and 13:29Z will only
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
     * be one hour as it is one minute short of two hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
     * This method operates in association with {@link TemporalUnit#between}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
     * The result of this method is a {@code long} representing the amount of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
     * the specified unit. By contrast, the result of {@code between} is an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
     * object that can be used directly in addition/subtraction:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1042
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
     *   long period = start.periodUntil(end, HOURS);   // this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
     *   dateTime.plus(HOURS.between(start, end));      // use in plus/minus
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
     * The calculation is implemented in this method for {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
     * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
     * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
     * Other {@code ChronoUnit} values will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
     * 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
  1053
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1054
     * passing {@code this} as the first argument and the input temporal as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
     * the second argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1056
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1057
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1058
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1059
     * @param endTime  the end time, which must be an {@code OffsetTime}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
     * @param unit  the unit to measure the period in, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1061
     * @return the amount of the period between this time and the end time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
     * @throws DateTimeException if the period cannot be calculated
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
    public long periodUntil(Temporal endTime, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
        if (endTime instanceof OffsetTime == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
            Objects.requireNonNull(endTime, "endTime");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
            throw new DateTimeException("Unable to calculate period between objects of two different types");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
            OffsetTime end = (OffsetTime) endTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
            long nanosUntil = end.toEpochNano() - toEpochNano();  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
            switch ((ChronoUnit) unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
                case NANOS: return nanosUntil;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
                case MICROS: return nanosUntil / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
                case MILLIS: return nanosUntil / 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
                case SECONDS: return nanosUntil / NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
                case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
                case HOURS: return nanosUntil / NANOS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
                case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
            throw new DateTimeException("Unsupported unit: " + unit.getName());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
        return unit.between(this, endTime).getAmount();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
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
     * Returns an offset date-time formed from this time at the specified date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
     * This combines this time with the specified date to form an {@code OffsetDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
     * All possible combinations of date and time are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1094
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1095
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1096
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1097
     * @param date  the date to combine with, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
     * @return the offset date-time formed from this time and the specified date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
    public OffsetDateTime atDate(LocalDate date) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
        return OffsetDateTime.of(date, time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1104
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1105
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
     * Converts this time to epoch nanos based on 1970-01-01Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1107
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1108
     * @return the epoch nanos value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1109
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1110
    private long toEpochNano() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
        long nod = time.toNanoOfDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1112
        long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1113
        return nod - offsetNanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1114
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1115
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
     * Compares this {@code OffsetTime} to another time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1120
     * The comparison is based first on the UTC equivalent instant, then on the local time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1121
     * It is "consistent with equals", as defined by {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1122
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
     * For example, the following is the comparator order:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1124
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
     * <li>{@code 10:30+01:00}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1126
     * <li>{@code 11:00+01:00}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
     * <li>{@code 12:00+02:00}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
     * <li>{@code 11:30+01:00}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
     * <li>{@code 12:00+01:00}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
     * <li>{@code 12:30+01:00}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1132
     * Values #2 and #3 represent the same instant on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
     * When two values represent the same instant, the local time is compared
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1134
     * to distinguish them. This step is needed to make the ordering
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1135
     * consistent with {@code equals()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1136
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1137
     * To compare the underlying local time of two {@code TemporalAccessor} instances,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1138
     * use {@link ChronoField#NANO_OF_DAY} as a comparator.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1139
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
     * @param other  the other time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
     * @return the comparator value, negative if less, positive if greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1142
     * @throws NullPointerException if {@code other} is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1143
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1144
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1145
    public int compareTo(OffsetTime other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1146
        if (offset.equals(other.offset)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
            return time.compareTo(other.time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1148
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1149
        int compare = Long.compare(toEpochNano(), other.toEpochNano());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1150
        if (compare == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1151
            compare = time.compareTo(other.time);
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 compare;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1154
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1155
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1156
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1157
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
     * Checks if the instant of this {@code OffsetTime} is after that of the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1159
     * specified time applying both times to a common date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1160
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1161
     * This method differs from the comparison in {@link #compareTo} in that it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1162
     * only compares the instant of the time. This is equivalent to converting both
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1163
     * times to an instant using the same date and comparing the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1164
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1165
     * @param other  the other time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1166
     * @return true if this is after the instant of the specified time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1167
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1168
    public boolean isAfter(OffsetTime other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1169
        return toEpochNano() > other.toEpochNano();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1170
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1171
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1172
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1173
     * Checks if the instant of this {@code OffsetTime} is before that of the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1174
     * specified time applying both times to a common date.
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
     * This method differs from the comparison in {@link #compareTo} in that it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1177
     * only compares the instant of the time. This is equivalent to converting both
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1178
     * times to an instant using the same date and comparing the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1179
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1180
     * @param other  the other time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1181
     * @return true if this is before the instant of the specified time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1182
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1183
    public boolean isBefore(OffsetTime other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1184
        return toEpochNano() < other.toEpochNano();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1185
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1186
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1187
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1188
     * Checks if the instant of this {@code OffsetTime} is equal to that of the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1189
     * specified time applying both times to a common date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1190
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1191
     * This method differs from the comparison in {@link #compareTo} and {@link #equals}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1192
     * in that it only compares the instant of the time. This is equivalent to converting both
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1193
     * times to an instant using the same date and comparing the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1194
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1195
     * @param other  the other time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1196
     * @return true if this is equal to the instant of the specified time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1197
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1198
    public boolean isEqual(OffsetTime other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1199
        return toEpochNano() == other.toEpochNano();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1200
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1201
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1202
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1203
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1204
     * Checks if this time is equal to another time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1205
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1206
     * The comparison is based on the local-time and the offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1207
     * To compare for the same instant on the time-line, use {@link #isEqual(OffsetTime)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1208
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1209
     * Only objects of type {@code OffsetTime} are compared, other types return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1210
     * To compare the underlying local time of two {@code TemporalAccessor} instances,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1211
     * use {@link ChronoField#NANO_OF_DAY} as a comparator.
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 obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1214
     * @return true if this is equal to the other time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1215
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1216
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1217
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1218
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1219
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1220
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1221
        if (obj instanceof OffsetTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1222
            OffsetTime other = (OffsetTime) obj;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1223
            return time.equals(other.time) && offset.equals(other.offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1224
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1225
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1226
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1227
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1228
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1229
     * A hash code for this time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1230
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1231
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1232
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1233
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1234
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1235
        return time.hashCode() ^ offset.hashCode();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1236
    }
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
     * Outputs this time as a {@code String}, such as {@code 10:15:30+01:00}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1241
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1242
     * The output will be one of the following ISO-8601 formats:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
     * <p><ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
     * <li>{@code HH:mmXXXXX}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1245
     * <li>{@code HH:mm:ssXXXXX}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1246
     * <li>{@code HH:mm:ss.SSSXXXXX}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1247
     * <li>{@code HH:mm:ss.SSSSSSXXXXX}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1248
     * <li>{@code HH:mm:ss.SSSSSSSSSXXXXX}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1249
     * </ul><p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1250
     * The format used will be the shortest that outputs the full value of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1251
     * the time where the omitted parts are implied to be zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1252
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1253
     * @return a string representation of this time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1254
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1255
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1256
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
        return time.toString() + offset.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1259
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1260
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1261
     * Outputs this time as a {@code String} using the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1262
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1263
     * This time will be passed to the formatter
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1264
     * {@link DateTimeFormatter#print(TemporalAccessor) print method}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1265
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1266
     * @param formatter  the formatter to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1267
     * @return the formatted time string, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1268
     * @throws DateTimeException if an error occurs during printing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1269
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1270
    public String toString(DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1271
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1272
        return formatter.print(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1273
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1274
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1275
    // -----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1276
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1277
     * Writes the object using a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1278
     * <a href="../../../serialized-form.html#java.time.temporal.Ser">dedicated serialized form</a>.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1279
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1280
     *  out.writeByte(2);  // identifies this as a OffsetDateTime
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1281
     *  out.writeObject(time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1282
     *  out.writeObject(offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1283
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1284
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1285
     * @return the instance of {@code Ser}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1286
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1287
    private Object writeReplace() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1288
        return new Ser(Ser.OFFSET_TIME_TYPE, this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1289
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1290
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1291
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1292
     * Defend against malicious streams.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1293
     * @return never
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1294
     * @throws InvalidObjectException always
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1295
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1296
    private Object readResolve() throws ObjectStreamException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1297
        throw new InvalidObjectException("Deserialization via serialization delegate");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1298
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1299
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1300
    void writeExternal(ObjectOutput out) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1301
        out.writeObject(time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1302
        out.writeObject(offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1303
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1304
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1305
    static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1306
        LocalTime time = (LocalTime) in.readObject();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1307
        ZoneOffset offset = (ZoneOffset) in.readObject();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1308
        return OffsetTime.of(time, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1309
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1310
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1311
}