jdk/src/java.base/share/classes/java/time/chrono/ChronoLocalDateTime.java
author redestad
Tue, 18 Apr 2017 18:25:09 +0200
changeset 44600 2027ac642982
parent 25859 3317bb8137f4
permissions -rw-r--r--
8178889: Move creation of AbstractChronology comparators to call sites Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     1
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     4
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    10
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    15
 * accompanied this code).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    16
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    20
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    23
 * questions.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    24
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    25
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    26
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    30
 * file:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    31
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    33
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    34
 * All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    35
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    38
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    41
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    45
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    49
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    61
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    62
package java.time.chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    63
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.EPOCH_DAY;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.NANO_OF_DAY;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    66
import static java.time.temporal.ChronoUnit.FOREVER;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoUnit.NANOS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    68
44600
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
    69
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    70
import java.time.DateTimeException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    71
import java.time.Instant;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    72
import java.time.LocalDateTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    73
import java.time.LocalTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    74
import java.time.ZoneId;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    75
import java.time.ZoneOffset;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    76
import java.time.format.DateTimeFormatter;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    77
import java.time.temporal.ChronoField;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    78
import java.time.temporal.ChronoUnit;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    79
import java.time.temporal.Temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    80
import java.time.temporal.TemporalAccessor;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    81
import java.time.temporal.TemporalAdjuster;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    82
import java.time.temporal.TemporalAmount;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    83
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
    84
import java.time.temporal.TemporalQueries;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    85
import java.time.temporal.TemporalQuery;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    86
import java.time.temporal.TemporalUnit;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    87
import java.time.zone.ZoneRules;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    88
import java.util.Comparator;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    89
import java.util.Objects;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    90
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    91
/**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    92
 * A date-time without a time-zone in an arbitrary chronology, intended
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    93
 * for advanced globalization use cases.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    94
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    95
 * <b>Most applications should declare method signatures, fields and variables
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    96
 * as {@link LocalDateTime}, not this interface.</b>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    97
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    98
 * A {@code ChronoLocalDateTime} is the abstract representation of a local date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    99
 * where the {@code Chronology chronology}, or calendar system, is pluggable.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   100
 * The date-time is defined in terms of fields expressed by {@link TemporalField},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   101
 * where most common implementations are defined in {@link ChronoField}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   102
 * The chronology defines how the calendar system operates and the meaning of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   103
 * the standard fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   104
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   105
 * <h3>When to use this interface</h3>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   106
 * The design of the API encourages the use of {@code LocalDateTime} rather than this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   107
 * interface, even in the case where the application needs to deal with multiple
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   108
 * calendar systems. The rationale for this is explored in detail in {@link ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   109
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   110
 * Ensure that the discussion in {@code ChronoLocalDate} has been read and understood
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   111
 * before using this interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   112
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   113
 * @implSpec
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   114
 * This interface must be implemented with care to ensure other classes operate correctly.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   115
 * All implementations that can be instantiated must be final, immutable and thread-safe.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   116
 * Subclasses should be Serializable wherever possible.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   117
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   118
 * @param <D> the concrete type for the date of this date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   119
 * @since 1.8
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   120
 */
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   121
public interface ChronoLocalDateTime<D extends ChronoLocalDate>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   122
        extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDateTime<?>> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   123
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   124
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   125
     * Gets a comparator that compares {@code ChronoLocalDateTime} in
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   126
     * time-line order ignoring the chronology.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   127
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   128
     * This comparator differs from the comparison in {@link #compareTo} in that it
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   129
     * only compares the underlying date-time and not the chronology.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   130
     * This allows dates in different calendar systems to be compared based
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   131
     * on the position of the date-time on the local time-line.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   132
     * The underlying comparison is equivalent to comparing the epoch-day and nano-of-day.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   133
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   134
     * @return a comparator that compares in time-line order ignoring the chronology
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   135
     * @see #isAfter
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   136
     * @see #isBefore
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   137
     * @see #isEqual
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   138
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   139
    static Comparator<ChronoLocalDateTime<?>> timeLineOrder() {
44600
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   140
        return (Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> & Serializable) (dateTime1, dateTime2) -> {
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   141
            int cmp = Long.compare(dateTime1.toLocalDate().toEpochDay(), dateTime2.toLocalDate().toEpochDay());
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   142
            if (cmp == 0) {
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   143
                cmp = Long.compare(dateTime1.toLocalTime().toNanoOfDay(), dateTime2.toLocalTime().toNanoOfDay());
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   144
            }
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   145
            return cmp;
2027ac642982 8178889: Move creation of AbstractChronology comparators to call sites
redestad
parents: 25859
diff changeset
   146
        };
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   147
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   148
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   149
    //-----------------------------------------------------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   150
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   151
     * Obtains an instance of {@code ChronoLocalDateTime} from a temporal object.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   152
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   153
     * This obtains a local date-time based on the specified temporal.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   154
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   155
     * which this factory converts to an instance of {@code ChronoLocalDateTime}.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   156
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   157
     * The conversion extracts and combines the chronology and the date-time
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   158
     * from the temporal object. The behavior is equivalent to using
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   159
     * {@link Chronology#localDateTime(TemporalAccessor)} with the extracted chronology.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   160
     * Implementations are permitted to perform optimizations such as accessing
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   161
     * those fields that are equivalent to the relevant objects.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   162
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   163
     * This method matches the signature of the functional interface {@link TemporalQuery}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   164
     * allowing it to be used as a query via method reference, {@code ChronoLocalDateTime::from}.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   165
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   166
     * @param temporal  the temporal object to convert, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   167
     * @return the date-time, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   168
     * @throws DateTimeException if unable to convert to a {@code ChronoLocalDateTime}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   169
     * @see Chronology#localDateTime(TemporalAccessor)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   170
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   171
    static ChronoLocalDateTime<?> from(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   172
        if (temporal instanceof ChronoLocalDateTime) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   173
            return (ChronoLocalDateTime<?>) temporal;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   174
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19030
diff changeset
   175
        Objects.requireNonNull(temporal, "temporal");
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   176
        Chronology chrono = temporal.query(TemporalQueries.chronology());
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   177
        if (chrono == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   178
            throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   179
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   180
        return chrono.localDateTime(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   181
    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   182
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   183
    //-----------------------------------------------------------------------
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   184
    /**
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   185
     * Gets the chronology of this date-time.
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   186
     * <p>
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   187
     * The {@code Chronology} represents the calendar system in use.
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   188
     * The era and other fields in {@link ChronoField} are defined by the chronology.
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   189
     *
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   190
     * @return the chronology, not null
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   191
     */
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   192
    default Chronology getChronology() {
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   193
        return toLocalDate().getChronology();
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   194
    }
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   195
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   196
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   197
     * Gets the local date part of this date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   198
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   199
     * This returns a local date with the same year, month and day
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   200
     * as this date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   201
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   202
     * @return the date part of this date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   203
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   204
    D toLocalDate() ;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   205
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   206
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   207
     * Gets the local time part of this date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   208
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   209
     * This returns a local time with the same hour, minute, second and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   210
     * nanosecond as this date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   211
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   212
     * @return the time part of this date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   213
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   214
    LocalTime toLocalTime();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   215
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   216
    /**
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   217
     * Checks if the specified field is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   218
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   219
     * This checks if the specified field can be queried on this date-time.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   220
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   221
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   222
     * methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   223
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   224
     * The set of supported fields is defined by the chronology and normally includes
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   225
     * all {@code ChronoField} date and time fields.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   226
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   227
     * If the field is not a {@code ChronoField}, then the result of this method
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   228
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   229
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   230
     * Whether the field is supported is determined by the field.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   231
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   232
     * @param field  the field to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   233
     * @return true if the field can be queried, false if not
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   234
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   235
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   236
    boolean isSupported(TemporalField field);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   237
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   238
    /**
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   239
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   240
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   241
     * This checks if the specified unit can be added to or subtracted from this date-time.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   242
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   243
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   244
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   245
     * The set of supported units is defined by the chronology and normally includes
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   246
     * all {@code ChronoUnit} units except {@code FOREVER}.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   247
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   248
     * If the unit is not a {@code ChronoUnit}, then the result of this method
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   249
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   250
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   251
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   252
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   253
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   254
     * @return true if the unit can be added/subtracted, false if not
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   255
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   256
    @Override
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   257
    default boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   258
        if (unit instanceof ChronoUnit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   259
            return unit != FOREVER;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   260
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   261
        return unit != null && unit.isSupportedBy(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   262
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   263
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   264
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   265
    // override for covariant return type
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   266
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   267
     * {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   268
     * @throws DateTimeException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   269
     * @throws ArithmeticException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   270
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   271
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   272
    default ChronoLocalDateTime<D> with(TemporalAdjuster adjuster) {
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   273
        return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.with(adjuster));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   274
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   275
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   276
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   277
     * {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   278
     * @throws DateTimeException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   279
     * @throws ArithmeticException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   280
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   281
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   282
    ChronoLocalDateTime<D> with(TemporalField field, long newValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   283
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   284
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   285
     * {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   286
     * @throws DateTimeException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   287
     * @throws ArithmeticException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   288
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   289
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   290
    default ChronoLocalDateTime<D> plus(TemporalAmount amount) {
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   291
        return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.plus(amount));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   292
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   293
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   294
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   295
     * {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   296
     * @throws DateTimeException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   297
     * @throws ArithmeticException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   298
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   299
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   300
    ChronoLocalDateTime<D> plus(long amountToAdd, TemporalUnit unit);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   301
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   302
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   303
     * {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   304
     * @throws DateTimeException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   305
     * @throws ArithmeticException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   306
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   307
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   308
    default ChronoLocalDateTime<D> minus(TemporalAmount amount) {
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   309
        return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.minus(amount));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   310
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   311
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   312
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   313
     * {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   314
     * @throws DateTimeException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   315
     * @throws ArithmeticException {@inheritDoc}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   316
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   317
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   318
    default ChronoLocalDateTime<D> minus(long amountToSubtract, TemporalUnit unit) {
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   319
        return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.minus(amountToSubtract, unit));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   320
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   321
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   322
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   323
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   324
     * Queries this date-time using the specified query.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   325
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   326
     * This queries this date-time using the specified query strategy object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   327
     * The {@code TemporalQuery} object defines the logic to be used to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   328
     * obtain the result. Read the documentation of the query to understand
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   329
     * what the result of this method will be.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   330
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   331
     * The result of this method is obtained by invoking the
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20795
diff changeset
   332
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   333
     * specified query passing {@code this} as the argument.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   334
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   335
     * @param <R> the type of the result
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   336
     * @param query  the query to invoke, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   337
     * @return the query result, null may be returned (defined by the query)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   338
     * @throws DateTimeException if unable to query (defined by the query)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   339
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   340
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   341
    @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   342
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   343
    default <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   344
        if (query == TemporalQueries.zoneId() || query == TemporalQueries.zone() || query == TemporalQueries.offset()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   345
            return null;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   346
        } else if (query == TemporalQueries.localTime()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   347
            return (R) toLocalTime();
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   348
        } else if (query == TemporalQueries.chronology()) {
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   349
            return (R) getChronology();
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20794
diff changeset
   350
        } else if (query == TemporalQueries.precision()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   351
            return (R) NANOS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   352
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   353
        // inline TemporalAccessor.super.query(query) as an optimization
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   354
        // non-JDK classes are not permitted to make this optimization
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   355
        return query.queryFrom(this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   356
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   357
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   358
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   359
     * Adjusts the specified temporal object to have the same date and time as this object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   360
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   361
     * This returns a temporal object of the same observable type as the input
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   362
     * with the date and time changed to be the same as this.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   363
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   364
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   365
     * twice, passing {@link ChronoField#EPOCH_DAY} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   366
     * {@link ChronoField#NANO_OF_DAY} as the fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   367
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   368
     * In most cases, it is clearer to reverse the calling pattern by using
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   369
     * {@link Temporal#with(TemporalAdjuster)}:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   370
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   371
     *   // these two lines are equivalent, but the second approach is recommended
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   372
     *   temporal = thisLocalDateTime.adjustInto(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   373
     *   temporal = temporal.with(thisLocalDateTime);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   374
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   375
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   376
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   377
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   378
     * @param temporal  the target object to be adjusted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   379
     * @return the adjusted object, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   380
     * @throws DateTimeException if unable to make the adjustment
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   381
     * @throws ArithmeticException if numeric overflow occurs
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   382
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   383
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   384
    default Temporal adjustInto(Temporal temporal) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   385
        return temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   386
                .with(EPOCH_DAY, toLocalDate().toEpochDay())
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   387
                .with(NANO_OF_DAY, toLocalTime().toNanoOfDay());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   388
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   389
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   390
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   391
     * Formats this date-time using the specified formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   392
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   393
     * This date-time will be passed to the formatter to produce a string.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   394
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   395
     * The default implementation must behave as follows:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   396
     * <pre>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   397
     *  return formatter.format(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   398
     * </pre>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   399
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   400
     * @param formatter  the formatter to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   401
     * @return the formatted date-time string, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   402
     * @throws DateTimeException if an error occurs during printing
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   403
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   404
    default String format(DateTimeFormatter formatter) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   405
        Objects.requireNonNull(formatter, "formatter");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   406
        return formatter.format(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   407
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   408
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   409
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   410
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   411
     * Combines this time with a time-zone to create a {@code ChronoZonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   412
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   413
     * This returns a {@code ChronoZonedDateTime} formed from this date-time at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   414
     * specified time-zone. The result will match this date-time as closely as possible.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   415
     * Time-zone rules, such as daylight savings, mean that not every local date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   416
     * is valid for the specified zone, thus the local date-time may be adjusted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   417
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   418
     * The local date-time is resolved to a single instant on the time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   419
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   420
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   421
     *<p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   422
     * In most cases, there is only one valid offset for a local date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   423
     * In the case of an overlap, where clocks are set back, there are two valid offsets.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   424
     * This method uses the earlier offset typically corresponding to "summer".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   425
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   426
     * In the case of a gap, where clocks jump forward, there is no valid offset.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   427
     * Instead, the local date-time is adjusted to be later by the length of the gap.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   428
     * For a typical one hour daylight savings change, the local date-time will be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   429
     * moved one hour later into the offset typically corresponding to "summer".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   430
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   431
     * To obtain the later offset during an overlap, call
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   432
     * {@link ChronoZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   433
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   434
     * @param zone  the time-zone to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   435
     * @return the zoned date-time formed from this date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   436
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   437
    ChronoZonedDateTime<D> atZone(ZoneId zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   438
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   439
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   440
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   441
     * Converts this date-time to an {@code Instant}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   442
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   443
     * This combines this local date-time and the specified offset to form
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   444
     * an {@code Instant}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   445
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   446
     * This default implementation calculates from the epoch-day of the date and the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   447
     * second-of-day of the time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   448
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   449
     * @param offset  the offset to use for the conversion, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   450
     * @return an {@code Instant} representing the same instant, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   451
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   452
    default Instant toInstant(ZoneOffset offset) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   453
        return Instant.ofEpochSecond(toEpochSecond(offset), toLocalTime().getNano());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   454
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   455
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   456
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   457
     * Converts this date-time to the number of seconds from the epoch
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   458
     * of 1970-01-01T00:00:00Z.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   459
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   460
     * This combines this local date-time and the specified offset to calculate the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   461
     * epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   462
     * Instants on the time-line after the epoch are positive, earlier are negative.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   463
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   464
     * This default implementation calculates from the epoch-day of the date and the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   465
     * second-of-day of the time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   466
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   467
     * @param offset  the offset to use for the conversion, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   468
     * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   469
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   470
    default long toEpochSecond(ZoneOffset offset) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   471
        Objects.requireNonNull(offset, "offset");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   472
        long epochDay = toLocalDate().toEpochDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   473
        long secs = epochDay * 86400 + toLocalTime().toSecondOfDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   474
        secs -= offset.getTotalSeconds();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   475
        return secs;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   476
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   477
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   478
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   479
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   480
     * Compares this date-time to another date-time, including the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   481
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   482
     * The comparison is based first on the underlying time-line date-time, then
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   483
     * on the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   484
     * It is "consistent with equals", as defined by {@link Comparable}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   485
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   486
     * For example, the following is the comparator order:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   487
     * <ol>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   488
     * <li>{@code 2012-12-03T12:00 (ISO)}</li>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   489
     * <li>{@code 2012-12-04T12:00 (ISO)}</li>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   490
     * <li>{@code 2555-12-04T12:00 (ThaiBuddhist)}</li>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   491
     * <li>{@code 2012-12-05T12:00 (ISO)}</li>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   492
     * </ol>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   493
     * Values #2 and #3 represent the same date-time on the time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   494
     * When two values represent the same date-time, the chronology ID is compared to distinguish them.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   495
     * This step is needed to make the ordering "consistent with equals".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   496
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   497
     * If all the date-time objects being compared are in the same chronology, then the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   498
     * additional chronology stage is not required and only the local date-time is used.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   499
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   500
     * This default implementation performs the comparison defined above.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   501
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   502
     * @param other  the other date-time to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   503
     * @return the comparator value, negative if less, positive if greater
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   504
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   505
    @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   506
    default int compareTo(ChronoLocalDateTime<?> other) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   507
        int cmp = toLocalDate().compareTo(other.toLocalDate());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   508
        if (cmp == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   509
            cmp = toLocalTime().compareTo(other.toLocalTime());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   510
            if (cmp == 0) {
20522
9a239bba214f 8024807: Add getChronlogy() to CLDT/CZDT
rriggs
parents: 20520
diff changeset
   511
                cmp = getChronology().compareTo(other.getChronology());
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   512
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   513
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   514
        return cmp;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   515
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   516
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   517
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   518
     * Checks if this date-time is after the specified date-time ignoring the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   519
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   520
     * This method differs from the comparison in {@link #compareTo} in that it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   521
     * only compares the underlying date-time and not the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   522
     * This allows dates in different calendar systems to be compared based
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   523
     * on the time-line position.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   524
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   525
     * This default implementation performs the comparison based on the epoch-day
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   526
     * and nano-of-day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   527
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   528
     * @param other  the other date-time to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   529
     * @return true if this is after the specified date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   530
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   531
    default boolean isAfter(ChronoLocalDateTime<?> other) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   532
        long thisEpDay = this.toLocalDate().toEpochDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   533
        long otherEpDay = other.toLocalDate().toEpochDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   534
        return thisEpDay > otherEpDay ||
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   535
            (thisEpDay == otherEpDay && this.toLocalTime().toNanoOfDay() > other.toLocalTime().toNanoOfDay());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   536
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   537
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   538
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   539
     * Checks if this date-time is before the specified date-time ignoring the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   540
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   541
     * This method differs from the comparison in {@link #compareTo} in that it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   542
     * only compares the underlying date-time and not the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   543
     * This allows dates in different calendar systems to be compared based
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   544
     * on the time-line position.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   545
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   546
     * This default implementation performs the comparison based on the epoch-day
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   547
     * and nano-of-day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   548
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   549
     * @param other  the other date-time to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   550
     * @return true if this is before the specified date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   551
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   552
    default boolean isBefore(ChronoLocalDateTime<?> other) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   553
        long thisEpDay = this.toLocalDate().toEpochDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   554
        long otherEpDay = other.toLocalDate().toEpochDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   555
        return thisEpDay < otherEpDay ||
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   556
            (thisEpDay == otherEpDay && this.toLocalTime().toNanoOfDay() < other.toLocalTime().toNanoOfDay());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   557
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   558
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   559
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   560
     * Checks if this date-time is equal to the specified date-time ignoring the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   561
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   562
     * This method differs from the comparison in {@link #compareTo} in that it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   563
     * only compares the underlying date and time and not the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   564
     * This allows date-times in different calendar systems to be compared based
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   565
     * on the time-line position.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   566
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   567
     * This default implementation performs the comparison based on the epoch-day
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   568
     * and nano-of-day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   569
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   570
     * @param other  the other date-time to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   571
     * @return true if the underlying date-time is equal to the specified date-time on the timeline
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   572
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   573
    default boolean isEqual(ChronoLocalDateTime<?> other) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   574
        // Do the time check first, it is cheaper than computing EPOCH day.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   575
        return this.toLocalTime().toNanoOfDay() == other.toLocalTime().toNanoOfDay() &&
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   576
               this.toLocalDate().toEpochDay() == other.toLocalDate().toEpochDay();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   577
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   578
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   579
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   580
     * Checks if this date-time is equal to another date-time, including the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   581
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   582
     * Compares this date-time with another ensuring that the date-time and chronology are the same.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   583
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   584
     * @param obj  the object to check, null returns false
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   585
     * @return true if this is equal to the other date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   586
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   587
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   588
    boolean equals(Object obj);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   589
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   590
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   591
     * A hash code for this date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   592
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   593
     * @return a suitable hash code
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   594
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   595
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   596
    int hashCode();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   597
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   598
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   599
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   600
     * Outputs this date-time as a {@code String}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   601
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   602
     * The output will include the full local date-time.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   603
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   604
     * @return a string representation of this date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   605
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   606
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   607
    String toString();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   608
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   609
}