jdk/src/share/classes/java/time/temporal/ChronoLocalDateTime.java
author sherman
Tue, 22 Jan 2013 20:59:21 -0800
changeset 15289 3ac550392e43
permissions -rw-r--r--
8003680: JSR 310 Date/Time API Summary: Integration of JSR310 Date/Time API for M6 Reviewed-by: alanb, naoto, dholmes Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, richard.warburton@gmail.com, misterm@gmail.com
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.EPOCH_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.NANO_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoUnit.NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.time.DateTimeException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import java.time.Instant;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import java.time.LocalDateTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import java.time.LocalTime;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import java.time.ZoneId;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.time.ZoneOffset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.time.format.DateTimeFormatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.time.zone.ZoneRules;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.util.Comparator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
 * A date-time without a time-zone in an arbitrary chronology, intended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
 * for advanced globalization use cases.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
 * <b>Most applications should declare method signatures, fields and variables
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 * as {@link LocalDateTime}, not this interface.</b>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
 * A {@code ChronoLocalDateTime} is the abstract representation of a local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
 * where the {@code Chrono chronology}, or calendar system, is pluggable.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
 * The date-time is defined in terms of fields expressed by {@link TemporalField},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
 * where most common implementations are defined in {@link ChronoField}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
 * The chronology defines how the calendar system operates and the meaning of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 * the standard fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * <h3>When to use this interface</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * The design of the API encourages the use of {@code LocalDateTime} rather than this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * interface, even in the case where the application needs to deal with multiple
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * calendar systems. The rationale for this is explored in detail in {@link ChronoLocalDate}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * Ensure that the discussion in {@code ChronoLocalDate} has been read and understood
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * before using this interface.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * This interface must be implemented with care to ensure other classes operate correctly.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * All implementations that can be instantiated must be final, immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * Subclasses should be Serializable wherever possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * @param <C> the chronology of this date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
public interface ChronoLocalDateTime<C extends Chrono<C>>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
        extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDateTime<?>> {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
     * Comparator for two {@code ChronoLocalDateTime} instances ignoring the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
     * This method differs from the comparison in {@link #compareTo} in that it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     * only compares the underlying date and not the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
     * This allows dates in different calendar systems to be compared based
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     * on the time-line position.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     * @see #isAfter
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     * @see #isBefore
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * @see #isEqual
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
    Comparator<ChronoLocalDateTime<?>> DATE_TIME_COMPARATOR =
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
            new Comparator<ChronoLocalDateTime<?>>() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
        public int compare(ChronoLocalDateTime<?> datetime1, ChronoLocalDateTime<?> datetime2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
            int cmp = Long.compare(datetime1.getDate().toEpochDay(), datetime2.getDate().toEpochDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
            if (cmp == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
                cmp = Long.compare(datetime1.getTime().toNanoOfDay(), datetime2.getTime().toNanoOfDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
            return cmp;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * Gets the local date part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * This returns a local date with the same year, month and day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     * as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * @return the date part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    ChronoLocalDate<C> getDate() ;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * Gets the local time part of this date-time.
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
     * This returns a local time with the same hour, minute, second and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     * nanosecond as this date-time.
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 time part of this date-time, 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
    LocalTime getTime();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
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
    // override for covariant return type
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * @throws DateTimeException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * @throws ArithmeticException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
    public default ChronoLocalDateTime<C> with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
        return getDate().getChrono().ensureChronoLocalDateTime(Temporal.super.with(adjuster));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     * {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     * @throws DateTimeException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * @throws ArithmeticException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
    ChronoLocalDateTime<C> with(TemporalField field, long newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * @throws DateTimeException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * @throws ArithmeticException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
    public default ChronoLocalDateTime<C> plus(TemporalAdder adder) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
        return getDate().getChrono().ensureChronoLocalDateTime(Temporal.super.plus(adder));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     * {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * @throws DateTimeException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
     * @throws ArithmeticException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
    ChronoLocalDateTime<C> plus(long amountToAdd, TemporalUnit unit);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * @throws DateTimeException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * @throws ArithmeticException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    public default ChronoLocalDateTime<C> minus(TemporalSubtractor subtractor) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
        return getDate().getChrono().ensureChronoLocalDateTime(Temporal.super.minus(subtractor));
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
     * {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * @throws DateTimeException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * @throws ArithmeticException {@inheritDoc}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    public default ChronoLocalDateTime<C> minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
        return getDate().getChrono().ensureChronoLocalDateTime(Temporal.super.minus(amountToSubtract, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * Queries this date-time using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * This queries this date-time using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
    public default <R> R query(TemporalQuery<R> query) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
        if (query == Queries.chrono()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
            return (R) getDate().getChrono();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
        if (query == Queries.precision()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
            return (R) NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
        // inline TemporalAccessor.super.query(query) as an optimization
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
        if (query == Queries.zoneId() || query == Queries.zone() || query == Queries.offset()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
            return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
        return query.queryFrom(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     * Adjusts the specified temporal object to have the same date and time as this object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     * with the date and time changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * twice, passing {@link ChronoField#EPOCH_DAY} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
     * {@link ChronoField#NANO_OF_DAY} as the fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     *   temporal = thisLocalDateTime.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     *   temporal = temporal.with(thisLocalDateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * </pre>
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
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
    public default Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
        return temporal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
                .with(EPOCH_DAY, getDate().toEpochDay())
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
                .with(NANO_OF_DAY, getTime().toNanoOfDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
     * Returns a zoned date-time formed from this date-time and the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * This creates a zoned date-time matching the input date-time as closely as possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * Time-zone rules, such as daylight savings, mean that not every local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     * is valid for the specified zone, thus the local date-time may be adjusted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * The local date-time is resolved to a single instant on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     *<p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * In most cases, there is only one valid offset for a local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     * In the case of an overlap, where clocks are set back, there are two valid offsets.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * This method uses the earlier offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * In the case of a gap, where clocks jump forward, there is no valid offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     * Instead, the local date-time is adjusted to be later by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * For a typical one hour daylight savings change, the local date-time will be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * moved one hour later into the offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
     * To obtain the later offset during an overlap, call
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
     * {@link ChronoZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
     * @param zone  the time-zone to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * @return the zoned date-time formed from this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
    ChronoZonedDateTime<C> atZone(ZoneId zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     * Converts this date-time to an {@code Instant}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     * This combines this local date-time and the specified offset to form
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     * an {@code Instant}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     * This default implementation calculates from the epoch-day of the date and the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     * second-of-day of the time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * @param offset  the offset to use for the conversion, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
     * @return an {@code Instant} representing the same instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
    public default Instant toInstant(ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
        return Instant.ofEpochSecond(toEpochSecond(offset), getTime().getNano());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * Converts this date-time to the number of seconds from the epoch
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     * This combines this local date-time and the specified offset to calculate the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * Instants on the time-line after the epoch are positive, earlier are negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * This default implementation calculates from the epoch-day of the date and the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     * second-of-day of the time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * @param offset  the offset to use for the conversion, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
    public default long toEpochSecond(ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
        Objects.requireNonNull(offset, "offset");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
        long epochDay = getDate().toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
        long secs = epochDay * 86400 + getTime().toSecondOfDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
        secs -= offset.getTotalSeconds();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
        return secs;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * Compares this date-time to another date-time, including the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * The comparison is based first on the underlying time-line date-time, then
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     * on the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     * It is "consistent with equals", as defined by {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     * For example, the following is the comparator order:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * <li>{@code 2012-12-03T12:00 (ISO)}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     * <li>{@code 2012-12-04T12:00 (ISO)}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
     * <li>{@code 2555-12-04T12:00 (ThaiBuddhist)}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
     * <li>{@code 2012-12-05T12:00 (ISO)}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     * Values #2 and #3 represent the same date-time on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
     * When two values represent the same date-time, the chronology ID is compared to distinguish them.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
     * This step is needed to make the ordering "consistent with equals".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
     * If all the date-time objects being compared are in the same chronology, then the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
     * additional chronology stage is not required and only the local date-time is used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
     * This default implementation performs the comparison defined above.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
     * @return the comparator value, negative if less, positive if greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
    public default int compareTo(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
        int cmp = getDate().compareTo(other.getDate());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
        if (cmp == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
            cmp = getTime().compareTo(other.getTime());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
            if (cmp == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
                cmp = getDate().getChrono().compareTo(other.getDate().getChrono());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
        return cmp;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     * Checks if this date-time is after the specified date-time ignoring the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * This method differs from the comparison in {@link #compareTo} in that it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * only compares the underlying date-time and not the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * This allows dates in different calendar systems to be compared based
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     * on the time-line position.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     * This default implementation performs the comparison based on the epoch-day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     * and nano-of-day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     * @return true if this is after the specified date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
    public default boolean isAfter(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
        long thisEpDay = this.getDate().toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
        long otherEpDay = other.getDate().toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
        return thisEpDay > otherEpDay ||
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
            (thisEpDay == otherEpDay && this.getTime().toNanoOfDay() > other.getTime().toNanoOfDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * Checks if this date-time is before the specified date-time ignoring the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * This method differs from the comparison in {@link #compareTo} in that it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * only compares the underlying date-time and not the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * This allows dates in different calendar systems to be compared based
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * on the time-line position.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     * This default implementation performs the comparison based on the epoch-day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * and nano-of-day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
     * @return true if this is before the specified date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
    public default boolean isBefore(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
        long thisEpDay = this.getDate().toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
        long otherEpDay = other.getDate().toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
        return thisEpDay < otherEpDay ||
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
            (thisEpDay == otherEpDay && this.getTime().toNanoOfDay() < other.getTime().toNanoOfDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * Checks if this date-time is equal to the specified date-time ignoring the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * This method differs from the comparison in {@link #compareTo} in that it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * only compares the underlying date and time and not the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * This allows date-times in different calendar systems to be compared based
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * on the time-line position.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * This default implementation performs the comparison based on the epoch-day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * and nano-of-day.
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 other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * @return true if the underlying date-time is equal to the specified date-time on the timeline
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
    public default boolean isEqual(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
        // Do the time check first, it is cheaper than computing EPOCH day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
        return this.getTime().toNanoOfDay() == other.getTime().toNanoOfDay() &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
               this.getDate().toEpochDay() == other.getDate().toEpochDay();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
     * Checks if this date-time is equal to another date-time, including the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
     * Compares this date-time with another ensuring that the date-time and chronology are the same.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     * @return true if this is equal to the other date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
    boolean equals(Object obj);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     * A hash code for this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
    int hashCode();
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
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
     * Outputs this date-time as a {@code String}.
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
     * The output will include the full local date-time and the chronology ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
     * @return a string representation of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
    String toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
     * Outputs this date-time as a {@code String} using the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * The default implementation must behave as follows:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     *  return formatter.print(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
     * @param formatter  the formatter to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
     * @return the formatted date-time string, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
     * @throws DateTimeException if an error occurs during printing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
    public default String toString(DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
        return formatter.print(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
}