jdk/src/java.base/share/classes/java/text/CalendarBuilder.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 17178 jdk/src/share/classes/java/text/CalendarBuilder.java@bf3ea41ef154
child 26219 1a19360ff122
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     1
/*
17178
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     4
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    10
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    15
 * accompanied this code).
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    16
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    20
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    23
 * questions.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    24
 */
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    25
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    26
package java.text;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    27
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    28
import java.util.Calendar;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    29
import static java.util.GregorianCalendar.*;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    30
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    31
/**
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    32
 * {@code CalendarBuilder} keeps field-value pairs for setting
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    33
 * the calendar fields of the given {@code Calendar}. It has the
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    34
 * {@link Calendar#FIELD_COUNT FIELD_COUNT}-th field for the week year
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    35
 * support. Also {@code ISO_DAY_OF_WEEK} is used to specify
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    36
 * {@code DAY_OF_WEEK} in the ISO day of week numbering.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    37
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    38
 * <p>{@code CalendarBuilder} retains the semantic of the pseudo
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    39
 * timestamp for fields. {@code CalendarBuilder} uses a single
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    40
 * int array combining fields[] and stamp[] of {@code Calendar}.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    41
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    42
 * @author Masayoshi Okutsu
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    43
 */
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    44
class CalendarBuilder {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    45
    /*
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    46
     * Pseudo time stamp constants used in java.util.Calendar
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    47
     */
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    48
    private static final int UNSET = 0;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    49
    private static final int COMPUTED = 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    50
    private static final int MINIMUM_USER_STAMP = 2;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    51
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    52
    private static final int MAX_FIELD = FIELD_COUNT + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    53
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    54
    public static final int WEEK_YEAR = FIELD_COUNT;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    55
    public static final int ISO_DAY_OF_WEEK = 1000; // pseudo field index
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    56
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    57
    // stamp[] (lower half) and field[] (upper half) combined
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    58
    private final int[] field;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    59
    private int nextStamp;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    60
    private int maxFieldIndex;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    61
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    62
    CalendarBuilder() {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    63
        field = new int[MAX_FIELD * 2];
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    64
        nextStamp = MINIMUM_USER_STAMP;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    65
        maxFieldIndex = -1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    66
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    67
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    68
    CalendarBuilder set(int index, int value) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    69
        if (index == ISO_DAY_OF_WEEK) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    70
            index = DAY_OF_WEEK;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    71
            value = toCalendarDayOfWeek(value);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    72
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    73
        field[index] = nextStamp++;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    74
        field[MAX_FIELD + index] = value;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    75
        if (index > maxFieldIndex && index < FIELD_COUNT) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    76
            maxFieldIndex = index;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    77
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    78
        return this;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    79
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    80
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    81
    CalendarBuilder addYear(int value) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    82
        field[MAX_FIELD + YEAR] += value;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    83
        field[MAX_FIELD + WEEK_YEAR] += value;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    84
        return this;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    85
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    86
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    87
    boolean isSet(int index) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    88
        if (index == ISO_DAY_OF_WEEK) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    89
            index = DAY_OF_WEEK;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    90
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    91
        return field[index] > UNSET;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    92
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    93
17178
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    94
    CalendarBuilder clear(int index) {
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    95
        if (index == ISO_DAY_OF_WEEK) {
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    96
            index = DAY_OF_WEEK;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    97
        }
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    98
        field[index] = UNSET;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    99
        field[MAX_FIELD + index] = 0;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   100
        return this;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   101
    }
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   102
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   103
    Calendar establish(Calendar cal) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   104
        boolean weekDate = isSet(WEEK_YEAR)
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   105
                            && field[WEEK_YEAR] > field[YEAR];
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   106
        if (weekDate && !cal.isWeekDateSupported()) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   107
            // Use YEAR instead
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   108
            if (!isSet(YEAR)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   109
                set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   110
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   111
            weekDate = false;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   112
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   113
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   114
        cal.clear();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   115
        // Set the fields from the min stamp to the max stamp so that
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   116
        // the field resolution works in the Calendar.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   117
        for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   118
            for (int index = 0; index <= maxFieldIndex; index++) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   119
                if (field[index] == stamp) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   120
                    cal.set(index, field[MAX_FIELD + index]);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   121
                    break;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   122
                }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   123
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   124
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   125
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   126
        if (weekDate) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   127
            int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   128
            int dayOfWeek = isSet(DAY_OF_WEEK) ?
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   129
                                field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   130
            if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   131
                if (dayOfWeek >= 8) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   132
                    dayOfWeek--;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   133
                    weekOfYear += dayOfWeek / 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   134
                    dayOfWeek = (dayOfWeek % 7) + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   135
                } else {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   136
                    while (dayOfWeek <= 0) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   137
                        dayOfWeek += 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   138
                        weekOfYear--;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   139
                    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   140
                }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   141
                dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   142
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   143
            cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   144
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   145
        return cal;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   146
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   147
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   148
    public String toString() {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   149
        StringBuilder sb = new StringBuilder();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   150
        sb.append("CalendarBuilder:[");
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   151
        for (int i = 0; i < field.length; i++) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   152
            if (isSet(i)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   153
                sb.append(i).append('=').append(field[MAX_FIELD + i]).append(',');
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   154
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   155
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   156
        int lastIndex = sb.length() - 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   157
        if (sb.charAt(lastIndex) == ',') {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   158
            sb.setLength(lastIndex);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   159
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   160
        sb.append(']');
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   161
        return sb.toString();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   162
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   163
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   164
    static int toISODayOfWeek(int calendarDayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   165
        return calendarDayOfWeek == SUNDAY ? 7 : calendarDayOfWeek - 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   166
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   167
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   168
    static int toCalendarDayOfWeek(int isoDayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   169
        if (!isValidDayOfWeek(isoDayOfWeek)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   170
            // adjust later for lenient mode
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   171
            return isoDayOfWeek;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   172
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   173
        return isoDayOfWeek == 7 ? SUNDAY : isoDayOfWeek + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   174
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   175
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   176
    static boolean isValidDayOfWeek(int dayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   177
        return dayOfWeek > 0 && dayOfWeek <= 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   178
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   179
}