jdk/src/share/classes/java/time/DayOfWeek.java
author sherman
Thu, 18 Jul 2013 11:02:02 -0700
changeset 19030 32f129cb6351
parent 17474 8c100beabcc0
child 20747 86a86a94b367
permissions -rw-r--r--
8016025: JSR 310 DateTime API Updates IV 8020418: Cleanup of -Xlint warnings in java.time 8016623: test/java/time/format/TestDateTimeTextProvider.java failing Summary: Integration of JSR310 Date/Time API update IV Reviewed-by: sherman Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com, chand.basha@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;
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;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.time.temporal.TemporalQuery;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    75
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.time.temporal.WeekFields;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
 * A day-of-week, such as 'Tuesday'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
 * {@code DayOfWeek} is an enum representing the 7 days of the week -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 * Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
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
 * 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
    87
 * 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
    88
 * 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
    89
 * to ensure code clarity.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 * 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
    92
 * Some locales also assign different numeric values to the days, declaring
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * 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
    94
 * See {@link WeekFields} for localized week-numbering.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * <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
    97
 * Use {@code getValue()} instead.</b>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * This enum represents a common concept that is found in many calendar systems.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * 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
   101
 * concept defined exactly equivalent to the ISO calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   103
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * This is an immutable and thread-safe enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
     * The singleton instance for the day-of-week of Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
     * This has the numeric value of {@code 1}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
    MONDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     * The singleton instance for the day-of-week of Tuesday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
     * This has the numeric value of {@code 2}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
    TUESDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     * The singleton instance for the day-of-week of Wednesday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
     * This has the numeric value of {@code 3}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
    WEDNESDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
     * The singleton instance for the day-of-week of Thursday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     * This has the numeric value of {@code 4}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
    THURSDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * The singleton instance for the day-of-week of Friday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * This has the numeric value of {@code 5}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
    FRIDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
     * The singleton instance for the day-of-week of Saturday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * This has the numeric value of {@code 6}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
    SATURDAY,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * The singleton instance for the day-of-week of Sunday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     * This has the numeric value of {@code 7}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    SUNDAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * Private cache of all the constants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
    private static final DayOfWeek[] ENUMS = DayOfWeek.values();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
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
     * Obtains an instance of {@code DayOfWeek} from an {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     * {@code DayOfWeek} is an enum representing the 7 days of the week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * This factory allows the enum to be obtained from the {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * 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
   157
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * @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
   159
     * @return the day-of-week singleton, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * @throws DateTimeException if the day-of-week is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
    public static DayOfWeek of(int dayOfWeek) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
        if (dayOfWeek < 1 || dayOfWeek > 7) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
            throw new DateTimeException("Invalid value for DayOfWeek: " + dayOfWeek);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
        return ENUMS[dayOfWeek - 1];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     * Obtains an instance of {@code DayOfWeek} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   173
     * This obtains a day-of-week based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   174
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   175
     * which this factory converts to an instance of {@code DayOfWeek}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * The conversion extracts the {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * 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
   181
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     * @return the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * @throws DateTimeException if unable to convert to a {@code DayOfWeek}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
    public static DayOfWeek from(TemporalAccessor temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
        if (temporal instanceof DayOfWeek) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
            return (DayOfWeek) temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
        return of(temporal.get(DAY_OF_WEEK));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
     * Gets the day-of-week {@code int} value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * The values are numbered following the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * See {@link WeekFields#dayOfWeek} for localized week-numbering.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     * @return the day-of-week, from 1 (Monday) to 7 (Sunday)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
    public int getValue() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
        return ordinal() + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * Gets the textual representation, such as 'Mon' or 'Friday'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   210
     * This returns the textual name used to identify the day-of-week,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   211
     * suitable for presentation to the user.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   212
     * The parameters control the style of the returned text and the locale.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * 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
   215
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * @param style  the length of the text required, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * @param locale  the locale to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @return the text value of the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   220
    public String getDisplayName(TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   221
        return new DateTimeFormatterBuilder().appendText(DAY_OF_WEEK, style).toFormatter(locale).format(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * 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
   229
     * If false, then calling the {@link #range(TemporalField) range} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * {@link #get(TemporalField) get} methods will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * If the field is {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} then
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     * this method returns true.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * 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
   237
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * @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
   243
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
            return field == DAY_OF_WEEK;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
        return field != null && field.isSupportedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * 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
   257
     * 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
   258
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * 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
   261
     * 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
   262
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * 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
   265
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * @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
   272
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
            return field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
        return TemporalAccessor.super.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     * 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
   284
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     * This queries this day-of-week for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * 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
   287
     * 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
   288
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * 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
   291
     * 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
   292
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * 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
   295
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * @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
   301
     * @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
   302
     *         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
   303
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   304
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
            return getValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
        return TemporalAccessor.super.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     * 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
   317
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     * This queries this day-of-week for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
     * 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
   320
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * 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
   323
     * 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
   324
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     * 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
   327
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
     * @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
   334
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
            return getValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
        } else if (field instanceof ChronoField) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   342
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   344
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * 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
   350
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
     * The calculation rolls around the end of the week from Sunday to Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     * The specified period may be negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * @param days  the days to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     * @return the resulting day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
    public DayOfWeek plus(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
        int amount = (int) (days % 7);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
        return ENUMS[(ordinal() + (amount + 7)) % 7];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
     * 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
   366
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     * The calculation rolls around the start of the year from Monday to Sunday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
     * The specified period may be negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
     * @param days  the days to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
     * @return the resulting day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
    public DayOfWeek minus(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
        return plus(-(days % 7));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * Queries this day-of-week using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * This queries this day-of-week using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
    public <R> R query(TemporalQuery<R> query) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   401
        if (query == TemporalQuery.precision()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
            return (R) DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
        return TemporalAccessor.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * Adjusts the specified temporal object to have this day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * with the day-of-week changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * passing {@link ChronoField#DAY_OF_WEEK} as the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * Note that this adjusts forwards or backwards within a Monday to Sunday week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * See {@link WeekFields#dayOfWeek} for localized week start days.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   417
     * See {@code TemporalAdjuster} for other adjusters with more control,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   418
     * such as {@code next(MONDAY)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
     *   temporal = thisDayOfWeek.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     *   temporal = temporal.with(thisDayOfWeek);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
     * For example, given a date that is a Wednesday, the following are output:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     *   dateOnWed.with(MONDAY);     // two days earlier
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     *   dateOnWed.with(TUESDAY);    // one day earlier
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
     *   dateOnWed.with(WEDNESDAY);  // same date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     *   dateOnWed.with(THURSDAY);   // one day later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     *   dateOnWed.with(FRIDAY);     // two days later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     *   dateOnWed.with(SATURDAY);   // three days later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     *   dateOnWed.with(SUNDAY);     // four days later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
        return temporal.with(DAY_OF_WEEK, getValue());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
}