jdk/src/java.base/share/classes/java/time/DayOfWeek.java
author ntv
Tue, 17 Nov 2015 10:44:22 -0500
changeset 33847 7857fc64d0c0
parent 25859 3317bb8137f4
permissions -rw-r--r--
8072746: LocalDate.isEra() should return IsoEra not Era Reviewed-by: rriggs, scolebourne
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoUnit.DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import java.time.format.DateTimeFormatterBuilder;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.time.format.TextStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import java.time.temporal.TemporalAdjuster;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
    74
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.time.temporal.TemporalQuery;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    76
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.time.temporal.WeekFields;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 * A day-of-week, such as 'Tuesday'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 * {@code DayOfWeek} is an enum representing the 7 days of the week -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
 * Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
 * In addition to the textual enum name, each day-of-week has an {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
 * The {@code int} value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
 * It is recommended that applications use the enum rather than the {@code int} value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
 * to ensure code clarity.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 * This enum provides access to the localized textual form of the day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * Some locales also assign different numeric values to the days, declaring
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * Sunday to have the value 1, however this class provides no support for this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * See {@link WeekFields} for localized week-numbering.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * <b>Do not use {@code ordinal()} to obtain the numeric representation of {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * Use {@code getValue()} instead.</b>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * This enum represents a common concept that is found in many calendar systems.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * As such, this enum may be used by any calendar system that has the day-of-week
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * concept defined exactly equivalent to the ISO calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   104
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * This is an immutable and thread-safe enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
     * The singleton instance for the day-of-week of Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
     * This has the numeric value of {@code 1}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
    MONDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
     * The singleton instance for the day-of-week of Tuesday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     * This has the numeric value of {@code 2}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
    TUESDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * The singleton instance for the day-of-week of Wednesday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * This has the numeric value of {@code 3}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
    WEDNESDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     * The singleton instance for the day-of-week of Thursday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     * This has the numeric value of {@code 4}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
    THURSDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * The singleton instance for the day-of-week of Friday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * This has the numeric value of {@code 5}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
    FRIDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * The singleton instance for the day-of-week of Saturday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * This has the numeric value of {@code 6}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
    SATURDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * The singleton instance for the day-of-week of Sunday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     * This has the numeric value of {@code 7}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    SUNDAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * Private cache of all the constants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
    private static final DayOfWeek[] ENUMS = DayOfWeek.values();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     * Obtains an instance of {@code DayOfWeek} from an {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * {@code DayOfWeek} is an enum representing the 7 days of the week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * This factory allows the enum to be obtained from the {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     * The {@code int} value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     * @param dayOfWeek  the day-of-week to represent, from 1 (Monday) to 7 (Sunday)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * @return the day-of-week singleton, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * @throws DateTimeException if the day-of-week is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
    public static DayOfWeek of(int dayOfWeek) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
        if (dayOfWeek < 1 || dayOfWeek > 7) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
            throw new DateTimeException("Invalid value for DayOfWeek: " + dayOfWeek);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
        return ENUMS[dayOfWeek - 1];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * Obtains an instance of {@code DayOfWeek} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   174
     * This obtains a day-of-week based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   175
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   176
     * which this factory converts to an instance of {@code DayOfWeek}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * The conversion extracts the {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     * allowing it to be used as a query via method reference, {@code DayOfWeek::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * @return the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * @throws DateTimeException if unable to convert to a {@code DayOfWeek}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    public static DayOfWeek from(TemporalAccessor temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
        if (temporal instanceof DayOfWeek) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
            return (DayOfWeek) temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
        }
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 19030
diff changeset
   191
        try {
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 19030
diff changeset
   192
            return of(temporal.get(DAY_OF_WEEK));
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 19030
diff changeset
   193
        } catch (DateTimeException ex) {
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 19030
diff changeset
   194
            throw new DateTimeException("Unable to obtain DayOfWeek from TemporalAccessor: " +
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 19030
diff changeset
   195
                    temporal + " of type " + temporal.getClass().getName(), ex);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 19030
diff changeset
   196
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
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
     * Gets the day-of-week {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     * The values are numbered following the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20795
diff changeset
   204
     * See {@link java.time.temporal.WeekFields#dayOfWeek()} for localized week-numbering.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * @return the day-of-week, from 1 (Monday) to 7 (Sunday)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
    public int getValue() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
        return ordinal() + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * Gets the textual representation, such as 'Mon' or 'Friday'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   216
     * This returns the textual name used to identify the day-of-week,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   217
     * suitable for presentation to the user.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   218
     * The parameters control the style of the returned text and the locale.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * @param style  the length of the text required, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * @param locale  the locale to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * @return the text value of the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   226
    public String getDisplayName(TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   227
        return new DateTimeFormatterBuilder().appendText(DAY_OF_WEEK, style).toFormatter(locale).format(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     * This checks if this day-of-week can be queried for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * If false, then calling the {@link #range(TemporalField) range} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * {@link #get(TemporalField) get} methods will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
     * If the field is {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} then
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * this method returns true.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   243
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * @return true if the field is supported on this day-of-week, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
            return field == DAY_OF_WEEK;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   255
        return field != null && field.isSupportedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * This day-of-week is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * If it is not possible to return the range, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * If the field is {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} then the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * range of the day-of-week, from 1 to 7, will be returned.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   268
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   271
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     * @throws DateTimeException if the range for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   278
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
            return field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
        return TemporalAccessor.super.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * Gets the value of the specified field from this day-of-week as an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20795
diff changeset
   291
     * This queries this day-of-week for the value of the specified field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * The returned value will always be within the valid range of values for the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * If the field is {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} then the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * value of the day-of-week, from 1 to 7, will be returned.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   298
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   301
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     * @return the value for the field, within the valid range of values
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   307
     * @throws DateTimeException if a value for the field cannot be obtained or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   308
     *         the value is outside the range of valid values for the field
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   309
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   310
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
            return getValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
        return TemporalAccessor.super.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * Gets the value of the specified field from this day-of-week as a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20795
diff changeset
   324
     * This queries this day-of-week for the value of the specified field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
     * If the field is {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} then the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * value of the day-of-week, from 1 to 7, will be returned.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   330
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   333
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     * @throws DateTimeException if a value for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   340
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
            return getValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
        } else if (field instanceof ChronoField) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   348
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   350
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * Returns the day-of-week that is the specified number of days after this one.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     * The calculation rolls around the end of the week from Sunday to Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     * The specified period may be negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * @param days  the days to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     * @return the resulting day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
    public DayOfWeek plus(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
        int amount = (int) (days % 7);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
        return ENUMS[(ordinal() + (amount + 7)) % 7];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
     * Returns the day-of-week that is the specified number of days before this one.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
     * The calculation rolls around the start of the year from Monday to Sunday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
     * The specified period may be negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
     * @param days  the days to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
     * @return the resulting day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
    public DayOfWeek minus(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
        return plus(-(days % 7));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * Queries this day-of-week using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
     * This queries this day-of-week using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
    public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
   407
        if (query == TemporalQueries.precision()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
            return (R) DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
        return TemporalAccessor.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * Adjusts the specified temporal object to have this day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * with the day-of-week changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     * passing {@link ChronoField#DAY_OF_WEEK} as the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * Note that this adjusts forwards or backwards within a Monday to Sunday week.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 20795
diff changeset
   422
     * See {@link java.time.temporal.WeekFields#dayOfWeek()} for localized week start days.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   423
     * See {@code TemporalAdjuster} for other adjusters with more control,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   424
     * such as {@code next(MONDAY)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     *   temporal = thisDayOfWeek.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     *   temporal = temporal.with(thisDayOfWeek);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * For example, given a date that is a Wednesday, the following are output:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     *   dateOnWed.with(MONDAY);     // two days earlier
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     *   dateOnWed.with(TUESDAY);    // one day earlier
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     *   dateOnWed.with(WEDNESDAY);  // same date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     *   dateOnWed.with(THURSDAY);   // one day later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     *   dateOnWed.with(FRIDAY);     // two days later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     *   dateOnWed.with(SATURDAY);   // three days later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     *   dateOnWed.with(SUNDAY);     // four days later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
        return temporal.with(DAY_OF_WEEK, getValue());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
}