jdk/src/share/classes/java/text/CalendarBuilder.java
author alanb
Thu, 18 Aug 2011 16:47:20 +0100
changeset 10347 1c9efe1ec7d3
parent 6491 cd1bcc5057f4
child 17178 bf3ea41ef154
permissions -rw-r--r--
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails Reviewed-by: forax, mduigou
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
/*
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    94
    Calendar establish(Calendar cal) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    95
        boolean weekDate = isSet(WEEK_YEAR)
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    96
                            && field[WEEK_YEAR] > field[YEAR];
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    97
        if (weekDate && !cal.isWeekDateSupported()) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    98
            // Use YEAR instead
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    99
            if (!isSet(YEAR)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   100
                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
   101
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   102
            weekDate = false;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   103
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   104
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   105
        cal.clear();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   106
        // 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
   107
        // 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
   108
        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
   109
            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
   110
                if (field[index] == stamp) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   111
                    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
   112
                    break;
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
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   115
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   116
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   117
        if (weekDate) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   118
            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
   119
            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
   120
                                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
   121
            if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   122
                if (dayOfWeek >= 8) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   123
                    dayOfWeek--;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   124
                    weekOfYear += dayOfWeek / 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   125
                    dayOfWeek = (dayOfWeek % 7) + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   126
                } else {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   127
                    while (dayOfWeek <= 0) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   128
                        dayOfWeek += 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   129
                        weekOfYear--;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   130
                    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   131
                }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   132
                dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   133
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   134
            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
   135
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   136
        return cal;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   137
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   138
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   139
    public String toString() {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   140
        StringBuilder sb = new StringBuilder();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   141
        sb.append("CalendarBuilder:[");
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   142
        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
   143
            if (isSet(i)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   144
                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
   145
            }
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
        int lastIndex = sb.length() - 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   148
        if (sb.charAt(lastIndex) == ',') {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   149
            sb.setLength(lastIndex);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   150
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   151
        sb.append(']');
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   152
        return sb.toString();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   153
    }
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
    static int toISODayOfWeek(int calendarDayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   156
        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
   157
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   158
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   159
    static int toCalendarDayOfWeek(int isoDayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   160
        if (!isValidDayOfWeek(isoDayOfWeek)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   161
            // adjust later for lenient mode
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   162
            return isoDayOfWeek;
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
        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
   165
    }
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
    static boolean isValidDayOfWeek(int dayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   168
        return dayOfWeek > 0 && dayOfWeek <= 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   169
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   170
}