jdk/src/share/classes/java/time/temporal/Queries.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.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
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    64
import static java.time.temporal.ChronoField.EPOCH_DAY;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    65
import static java.time.temporal.ChronoField.NANO_OF_DAY;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    68
import java.time.LocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    69
import java.time.LocalTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    70
import java.time.OffsetDateTime;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import java.time.ZoneId;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import java.time.ZoneOffset;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    73
import java.time.ZonedDateTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    74
import java.time.chrono.Chronology;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
 * Common implementations of {@code TemporalQuery}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
 * This class provides common implementations of {@link TemporalQuery}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
 * These queries are primarily used as optimizations, allowing the internals
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
 * of other objects to be extracted effectively. Note that application code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 * can also use the {@code from(TemporalAccessor)} method on most temporal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
 * objects as a method reference matching the query interface, such as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 * {@code LocalDate::from} and {@code ZoneId::from}.
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
 * There are two equivalent ways of using a {@code TemporalQuery}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
 * The first is to invoke the method on the interface directly.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
 * The second is to use {@link TemporalAccessor#query(TemporalQuery)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
 * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
 *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 *   dateTime = query.queryFrom(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 *   dateTime = dateTime.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * It is recommended to use the second approach, {@code query(TemporalQuery)},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * as it is a lot clearer to read in code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * This is a thread-safe utility class.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * All returned adjusters are immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
public final class Queries {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
    // note that it is vital that each method supplies a constant, not a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
    // calculated value, as they will be checked for using ==
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
    // it is also vital that each constant is different (due to the == checking)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
    // as such, alterations to use lambdas must be done with extreme care
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
     * Private constructor since this is a utility class.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
    private Queries() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
    // special constants should be used to extract information from a TemporalAccessor
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
    // that cannot be derived in other ways
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
    // Javadoc added here, so as to pretend they are more normal than they really are
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     * A strict query for the {@code ZoneId}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * This queries a {@code TemporalAccessor} for the zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     * The zone is only returned if the date-time conceptually contains a {@code ZoneId}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
     * It will not be returned if the date-time only conceptually has an {@code ZoneOffset}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   126
     * Thus a {@link ZonedDateTime} will return the result of {@code getZone()},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   127
     * but an {@link OffsetDateTime} will return null.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     * In most cases, applications should use {@link #ZONE} as this query is too strict.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * The result from JDK classes implementing {@code TemporalAccessor} is as follows:<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * {@code LocalDate} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * {@code LocalTime} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     * {@code LocalDateTime} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     * {@code ZonedDateTime} returns the associated zone<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
     * {@code OffsetTime} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * {@code OffsetDateTime} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * {@code ChronoLocalDate} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * {@code ChronoLocalDateTime} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     * {@code ChronoZonedDateTime} returns the associated zone<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * {@code Era} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * {@code DayOfWeek} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     * {@code Month} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
     * {@code Year} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
     * {@code YearMonth} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * {@code MonthDay} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * {@code ZoneOffset} returns null<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     * {@code Instant} returns null<br>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   149
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   150
     * @return a query that can obtain the zone ID of a temporal, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
    public static final TemporalQuery<ZoneId> zoneId() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
        return ZONE_ID;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   155
    static final TemporalQuery<ZoneId> ZONE_ID = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   156
        return temporal.query(ZONE_ID);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   160
     * A query for the {@code Chronology}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * This queries a {@code TemporalAccessor} for the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * If the target {@code TemporalAccessor} represents a date, or part of a date,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * then it should return the chronology that the date is expressed in.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * As a result of this definition, objects only representing time, such as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * {@code LocalTime}, will return null.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     * The result from JDK classes implementing {@code TemporalAccessor} is as follows:<br>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   169
     * {@code LocalDate} returns {@code IsoChronology.INSTANCE}<br>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     * {@code LocalTime} returns null (does not represent a date)<br>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   171
     * {@code LocalDateTime} returns {@code IsoChronology.INSTANCE}<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   172
     * {@code ZonedDateTime} returns {@code IsoChronology.INSTANCE}<br>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     * {@code OffsetTime} returns null (does not represent a date)<br>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   174
     * {@code OffsetDateTime} returns {@code IsoChronology.INSTANCE}<br>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     * {@code ChronoLocalDate} returns the associated chronology<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * {@code ChronoLocalDateTime} returns the associated chronology<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * {@code ChronoZonedDateTime} returns the associated chronology<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * {@code Era} returns the associated chronology<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * {@code DayOfWeek} returns null (shared across chronologies)<br>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   180
     * {@code Month} returns {@code IsoChronology.INSTANCE}<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   181
     * {@code Year} returns {@code IsoChronology.INSTANCE}<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   182
     * {@code YearMonth} returns {@code IsoChronology.INSTANCE}<br>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   183
     * {@code MonthDay} returns null {@code IsoChronology.INSTANCE}<br>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * {@code ZoneOffset} returns null (does not represent a date)<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * {@code Instant} returns null (does not represent a date)<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   187
     * The method {@link Chronology#from(TemporalAccessor)} can be used as a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   188
     * {@code TemporalQuery} via a method reference, {@code Chronology::from}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * That method is equivalent to this query, except that it throws an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
     * exception if a chronology cannot be obtained.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   191
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
     * @return a query that can obtain the chronology of a temporal, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   194
    public static final TemporalQuery<Chronology> chronology() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
        return CHRONO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   197
    static final TemporalQuery<Chronology> CHRONO = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   198
        return temporal.query(CHRONO);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
     * A query for the smallest supported unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * This queries a {@code TemporalAccessor} for the time precision.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * If the target {@code TemporalAccessor} represents a consistent or complete date-time,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * date or time then this must return the smallest precision actually supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * Note that fields such as {@code NANO_OF_DAY} and {@code NANO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * are defined to always return ignoring the precision, thus this is the only
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * way to find the actual smallest supported unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * For example, were {@code GregorianCalendar} to implement {@code TemporalAccessor}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * it would return a precision of {@code MILLIS}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * The result from JDK classes implementing {@code TemporalAccessor} is as follows:<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * {@code LocalDate} returns {@code DAYS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * {@code LocalTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * {@code LocalDateTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * {@code ZonedDateTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * {@code OffsetTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * {@code OffsetDateTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * {@code ChronoLocalDate} returns {@code DAYS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * {@code ChronoLocalDateTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * {@code ChronoZonedDateTime} returns {@code NANOS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * {@code Era} returns {@code ERAS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * {@code DayOfWeek} returns {@code DAYS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * {@code Month} returns {@code MONTHS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * {@code Year} returns {@code YEARS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * {@code YearMonth} returns {@code MONTHS}<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * {@code MonthDay} returns null (does not represent a complete date or time)<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * {@code ZoneOffset} returns null (does not represent a date or time)<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * {@code Instant} returns {@code NANOS}<br>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   231
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   232
     * @return a query that can obtain the precision of a temporal, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   234
    public static final TemporalQuery<TemporalUnit> precision() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
        return PRECISION;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   237
    static final TemporalQuery<TemporalUnit> PRECISION = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   238
        return temporal.query(PRECISION);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
    // non-special constants are standard queries that derive information from other information
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     * A lenient query for the {@code ZoneId}, falling back to the {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     * This queries a {@code TemporalAccessor} for the zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     * It first tries to obtain the zone, using {@link #zoneId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * If that is not found it tries to obtain the {@link #offset()}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
     * Thus a {@link ZonedDateTime} will return the result of {@code getZone()},
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   250
     * while an {@link OffsetDateTime} will return the result of {@code getOffset()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * In most cases, applications should use this query rather than {@code #zoneId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     * The method {@link ZoneId#from(TemporalAccessor)} can be used as a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
     * {@code TemporalQuery} via a method reference, {@code ZoneId::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * That method is equivalent to this query, except that it throws an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * exception if a zone cannot be obtained.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   259
     * @return a query that can obtain the zone ID or offset of a temporal, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
    public static final TemporalQuery<ZoneId> zone() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
        return ZONE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   264
    static final TemporalQuery<ZoneId> ZONE = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   265
        ZoneId zone = temporal.query(ZONE_ID);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   266
        return (zone != null ? zone : temporal.query(OFFSET));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   270
     * A query for {@code ZoneOffset} returning null if not found.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   272
     * This returns a {@code TemporalQuery} that can be used to query a temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   273
     * object for the offset. The query will return null if the temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   274
     * object cannot supply an offset.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
     * The query implementation examines the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     * field and uses it to create a {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
     * The method {@link ZoneOffset#from(TemporalAccessor)} can be used as a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     * {@code TemporalQuery} via a method reference, {@code ZoneOffset::from}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   281
     * This query and {@code ZoneOffset::from} will return the same result if the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
     * temporal object contains an offset. If the temporal object does not contain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   283
     * an offset, then the method reference will throw an exception, whereas this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   284
     * query will return null.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   285
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   286
     * @return a query that can obtain the offset of a temporal, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
    public static final TemporalQuery<ZoneOffset> offset() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
        return OFFSET;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   291
    static final TemporalQuery<ZoneOffset> OFFSET = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   292
        if (temporal.isSupported(OFFSET_SECONDS)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   293
            return ZoneOffset.ofTotalSeconds(temporal.get(OFFSET_SECONDS));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   295
        return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   296
    };
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   297
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   298
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   299
     * A query for {@code LocalDate} returning null if not found.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   300
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   301
     * This returns a {@code TemporalQuery} that can be used to query a temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   302
     * object for the local date. The query will return null if the temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   303
     * object cannot supply a local date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   304
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   305
     * The query implementation examines the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   306
     * field and uses it to create a {@code LocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   307
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   308
     * The method {@link ZoneOffset#from(TemporalAccessor)} can be used as a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   309
     * {@code TemporalQuery} via a method reference, {@code LocalDate::from}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   310
     * This query and {@code LocalDate::from} will return the same result if the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   311
     * temporal object contains a date. If the temporal object does not contain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   312
     * a date, then the method reference will throw an exception, whereas this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   313
     * query will return null.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   314
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   315
     * @return a query that can obtain the date of a temporal, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   316
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   317
    public static final TemporalQuery<LocalDate> localDate() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   318
        return LOCAL_DATE;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   319
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   320
    static final TemporalQuery<LocalDate> LOCAL_DATE = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   321
        if (temporal.isSupported(EPOCH_DAY)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   322
            return LocalDate.ofEpochDay(temporal.getLong(EPOCH_DAY));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   323
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   324
        return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   325
    };
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   326
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   327
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   328
     * A query for {@code LocalTime} returning null if not found.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   329
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   330
     * This returns a {@code TemporalQuery} that can be used to query a temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   331
     * object for the local time. The query will return null if the temporal
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   332
     * object cannot supply a local time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   333
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   334
     * The query implementation examines the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   335
     * field and uses it to create a {@code LocalTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   336
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   337
     * The method {@link ZoneOffset#from(TemporalAccessor)} can be used as a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   338
     * {@code TemporalQuery} via a method reference, {@code LocalTime::from}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   339
     * This query and {@code LocalTime::from} will return the same result if the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   340
     * temporal object contains a time. If the temporal object does not contain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   341
     * a time, then the method reference will throw an exception, whereas this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   342
     * query will return null.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   343
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   344
     * @return a query that can obtain the time of a temporal, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   345
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   346
    public static final TemporalQuery<LocalTime> localTime() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   347
        return LOCAL_TIME;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   348
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   349
    static final TemporalQuery<LocalTime> LOCAL_TIME = (temporal) -> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   350
        if (temporal.isSupported(NANO_OF_DAY)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   351
            return LocalTime.ofNanoOfDay(temporal.getLong(NANO_OF_DAY));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   352
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   353
        return null;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
}