Example usage for java.text DateFormatSymbols getShortWeekdays

List of usage examples for java.text DateFormatSymbols getShortWeekdays

Introduction

In this page you can find the example usage for java.text DateFormatSymbols getShortWeekdays.

Prototype

public String[] getShortWeekdays() 

Source Link

Document

Gets short weekday strings.

Usage

From source file:com.alkacon.opencms.calendar.CmsCalendarMonthBean.java

/**
 * Builds the HTML output to create a basic calendar month overview.<p>
 * //from   w ww .j a v  a  2  s  .  co m
 * This method serves as a simple example to create a basic html calendar monthly view.<p>
 * 
 * @param year the year of the month to display
 * @param month the month to display 
 * @param calendarLocale the Locale for the calendar to determine the start day of the weeks
 * @param showNavigation if true, navigation links to switch the month are created, otherwise not
 * @return the HTML output to create a basic calendar month overview
 */
public String buildCalendarMonth(int year, int month, Locale calendarLocale, boolean showNavigation) {

    StringBuffer result = new StringBuffer(1024);

    Map dates = getMonthDaysMatrix(year, month, calendarLocale);
    Map monthEntries = getEntriesForMonth(year, month);

    // calculate the start day of the week
    Calendar calendar = new GregorianCalendar(calendarLocale);
    int weekStart = calendar.getFirstDayOfWeek();
    // store current calendar date
    Calendar currentCalendar = (Calendar) calendar.clone();

    // init the date format symbols
    DateFormatSymbols calendarSymbols = new DateFormatSymbols(calendarLocale);

    // open the table
    result.append("<table class=\"");
    result.append(getStyle().getStyleTable());
    result.append("\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");

    // create the calendar navigation row
    result.append(buildMonthNavigation(year, month, currentCalendar, calendarLocale, showNavigation));

    // create the week day row
    result.append("<tr>\n");
    int currWeekDay = weekStart;
    for (int i = 1; i <= 7; i++) {

        result.append("\t<td class=\"");
        result.append(getStyle().getStyleWeekdays());
        result.append("\">");
        result.append(calendarSymbols.getShortWeekdays()[currWeekDay]);
        result.append("</td>\n");
        // check if we are at end of week
        if (currWeekDay == Calendar.SATURDAY) {
            currWeekDay = 0;
        }

        currWeekDay++;
    }
    result.append("</tr>\n");

    // now create the entry rows
    result.append("<tr>\n");

    // iterate the index entries of the matrix
    Iterator i = dates.keySet().iterator();
    while (i.hasNext()) {
        Integer index = (Integer) i.next();
        result.append("\t<td class=\"");
        Calendar currDay = (Calendar) dates.get(index);
        if (currDay != null) {
            // current index represents a day, create day output
            String styleDayCell = getStyle().getStyleDay();
            if (isCurrentDay(currentCalendar, currDay)) {
                // for the current day, use another cell style
                styleDayCell = getStyle().getStyleDayCurrent();
            }
            // get entries for the day
            List dayEntries = (List) monthEntries.get(currDay.getTime());
            if (dayEntries.size() > 0) {
                // current day has calendar entries
                int weekdayStatus = 0;
                int holidayEntries = 0;
                int commonEntries = dayEntries.size();
                StringBuffer dayText = new StringBuffer(128);
                // check all entries for special weekday status entries
                for (int k = 0; k < commonEntries; k++) {
                    CmsCalendarEntry entry = (CmsCalendarEntry) dayEntries.get(k);
                    int entryWeekdayStatus = entry.getEntryData().getWeekdayStatus();
                    if (entryWeekdayStatus > 0) {
                        // entry is a special weekday
                        holidayEntries++;
                        // append special day info to title info
                        dayText.append(entry.getEntryData().getTitle());
                        dayText.append(" - ");
                        if (entryWeekdayStatus > weekdayStatus) {
                            // increase the status of the weekday
                            weekdayStatus = entryWeekdayStatus;
                        }
                    }
                }
                // calculate the count of common calendar entries
                commonEntries = commonEntries - holidayEntries;
                // determine the CSS class to use
                String dayStyle = getWeekdayStyle(currDay.get(Calendar.DAY_OF_WEEK), weekdayStatus);
                result.append(styleDayCell);
                result.append("\" title=\"");
                result.append(dayText);
                // check the number of common entries and generate output of entry count
                if (commonEntries <= 0) {
                    // no entry found
                    result.append(getMessages().key("calendar.entries.count.none"));
                } else if (commonEntries == 1) {
                    // one entry found
                    result.append(getMessages().key("calendar.entries.count.one"));
                } else {
                    // more than one entry found
                    result.append(getMessages().key("calendar.entries.count.more",
                            new String[] { String.valueOf(commonEntries) }));
                }
                result.append("\">");
                if (commonEntries > 0) {
                    // common entries present, create link to the overview page 
                    result.append("<a href=\"");
                    result.append(createLink(currDay, m_viewUri, true, -1));
                    result.append("\" class=\"");
                    result.append(getStyle().getStyleDayEntryLink());
                    result.append("\">");
                }
                result.append("<span class=\"");
                result.append(dayStyle);
                result.append("\">");
                result.append(currDay.get(Calendar.DAY_OF_MONTH));
                result.append("</span>");
                if (commonEntries > 0) {
                    // common entries present, close link
                    result.append("</a>");
                }
            } else {
                // current day has no entries
                result.append(styleDayCell);
                result.append("\" title=\"");
                result.append(getMessages().key("calendar.entries.count.none"));
                result.append("\">");
                result.append("<span class=\"");
                result.append(getWeekdayStyle(currDay.get(Calendar.DAY_OF_WEEK),
                        I_CmsCalendarEntryData.WEEKDAYSTATUS_WORKDAY));
                result.append("\">");
                result.append(currDay.get(Calendar.DAY_OF_MONTH));
                result.append("</span>");
            }
        } else {
            // this is an empty cell
            result.append(getStyle().getStyleDayEmpty());
            result.append("\">");
        }
        result.append("</td>\n");
        if ((index.intValue() % 7) == 0) {
            // append closing row tag
            result.append("</tr>\n");
            if (i.hasNext()) {
                // open next row if more elements are present
                result.append("<tr>\n");
            }
        }
    }

    // close the table
    result.append("</table>");
    return result.toString();
}

From source file:com.application.utils.FastDatePrinter.java

/**
 * <p>Returns a list of Rules given a pattern.</p>
 *
 * @return a {@code List} of Rule objects
 * @throws IllegalArgumentException if pattern is invalid
 *///from ww w.jav  a  2s  .  com
protected List<Rule> parsePattern() {
    final DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
    final List<Rule> rules = new ArrayList<Rule>();

    final String[] ERAs = symbols.getEras();
    final String[] months = symbols.getMonths();
    final String[] shortMonths = symbols.getShortMonths();
    final String[] weekdays = symbols.getWeekdays();
    final String[] shortWeekdays = symbols.getShortWeekdays();
    final String[] AmPmStrings = symbols.getAmPmStrings();

    final int length = mPattern.length();
    final int[] indexRef = new int[1];

    for (int i = 0; i < length; i++) {
        indexRef[0] = i;
        final String token = parseToken(mPattern, indexRef);
        i = indexRef[0];

        final int tokenLen = token.length();
        if (tokenLen == 0) {
            break;
        }

        Rule rule;
        final char c = token.charAt(0);

        switch (c) {
        case 'G': // era designator (text)
            rule = new TextField(Calendar.ERA, ERAs);
            break;
        case 'y': // year (number)
            if (tokenLen == 2) {
                rule = TwoDigitYearField.INSTANCE;
            } else {
                rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
            }
            break;
        case 'M': // month in year (text and number)
            if (tokenLen >= 4) {
                rule = new TextField(Calendar.MONTH, months);
            } else if (tokenLen == 3) {
                rule = new TextField(Calendar.MONTH, shortMonths);
            } else if (tokenLen == 2) {
                rule = TwoDigitMonthField.INSTANCE;
            } else {
                rule = UnpaddedMonthField.INSTANCE;
            }
            break;
        case 'd': // day in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
            break;
        case 'h': // hour in am/pm (number, 1..12)
            rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
            break;
        case 'H': // hour in day (number, 0..23)
            rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
            break;
        case 'm': // minute in hour (number)
            rule = selectNumberRule(Calendar.MINUTE, tokenLen);
            break;
        case 's': // second in minute (number)
            rule = selectNumberRule(Calendar.SECOND, tokenLen);
            break;
        case 'S': // millisecond (number)
            rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
            break;
        case 'E': // day in week (text)
            rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
            break;
        case 'D': // day in year (number)
            rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
            break;
        case 'F': // day of week in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
            break;
        case 'w': // week in year (number)
            rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
            break;
        case 'W': // week in month (number)
            rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
            break;
        case 'a': // am/pm marker (text)
            rule = new TextField(Calendar.AM_PM, AmPmStrings);
            break;
        case 'k': // hour in day (1..24)
            rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
            break;
        case 'K': // hour in am/pm (0..11)
            rule = selectNumberRule(Calendar.HOUR, tokenLen);
            break;
        case 'z': // time zone (text)
            if (tokenLen >= 4) {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG);
            } else {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT);
            }
            break;
        case 'Z': // time zone (value)
            if (tokenLen == 1) {
                rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
            } else {
                rule = TimeZoneNumberRule.INSTANCE_COLON;
            }
            break;
        case '\'': // literal text
            final String sub = token.substring(1);
            if (sub.length() == 1) {
                rule = new CharacterLiteral(sub.charAt(0));
            } else {
                rule = new StringLiteral(sub);
            }
            break;
        default:
            throw new IllegalArgumentException("Illegal pattern component: " + token);
        }

        rules.add(rule);
    }

    return rules;
}

From source file:org.sqlite.date.FastDatePrinter.java

/**
 * <p>Returns a list of Rules given a pattern.</p>
 *
 * @return a {@code List} of Rule objects
 * @throws IllegalArgumentException if pattern is invalid
 *//*from   ww w.j  a v a 2  s  .  c  o m*/
protected List<Rule> parsePattern() {
    final DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
    final List<Rule> rules = new ArrayList<Rule>();

    final String[] ERAs = symbols.getEras();
    final String[] months = symbols.getMonths();
    final String[] shortMonths = symbols.getShortMonths();
    final String[] weekdays = symbols.getWeekdays();
    final String[] shortWeekdays = symbols.getShortWeekdays();
    final String[] AmPmStrings = symbols.getAmPmStrings();

    final int length = mPattern.length();
    final int[] indexRef = new int[1];

    for (int i = 0; i < length; i++) {
        indexRef[0] = i;
        final String token = parseToken(mPattern, indexRef);
        i = indexRef[0];

        final int tokenLen = token.length();
        if (tokenLen == 0) {
            break;
        }

        Rule rule;
        final char c = token.charAt(0);

        switch (c) {
        case 'G': // era designator (text)
            rule = new TextField(Calendar.ERA, ERAs);
            break;
        case 'y': // year (number)
            if (tokenLen == 2) {
                rule = TwoDigitYearField.INSTANCE;
            } else {
                rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
            }
            break;
        case 'M': // month in year (text and number)
            if (tokenLen >= 4) {
                rule = new TextField(Calendar.MONTH, months);
            } else if (tokenLen == 3) {
                rule = new TextField(Calendar.MONTH, shortMonths);
            } else if (tokenLen == 2) {
                rule = TwoDigitMonthField.INSTANCE;
            } else {
                rule = UnpaddedMonthField.INSTANCE;
            }
            break;
        case 'd': // day in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
            break;
        case 'h': // hour in am/pm (number, 1..12)
            rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
            break;
        case 'H': // hour in day (number, 0..23)
            rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
            break;
        case 'm': // minute in hour (number)
            rule = selectNumberRule(Calendar.MINUTE, tokenLen);
            break;
        case 's': // second in minute (number)
            rule = selectNumberRule(Calendar.SECOND, tokenLen);
            break;
        case 'S': // millisecond (number)
            rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
            break;
        case 'E': // day in week (text)
            rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
            break;
        case 'D': // day in year (number)
            rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
            break;
        case 'F': // day of week in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
            break;
        case 'w': // week in year (number)
            rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
            break;
        case 'W': // week in month (number)
            rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
            break;
        case 'a': // am/pm marker (text)
            rule = new TextField(Calendar.AM_PM, AmPmStrings);
            break;
        case 'k': // hour in day (1..24)
            rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
            break;
        case 'K': // hour in am/pm (0..11)
            rule = selectNumberRule(Calendar.HOUR, tokenLen);
            break;
        case 'X': // ISO 8601 
            rule = Iso8601_Rule.getRule(tokenLen);
            break;
        case 'z': // time zone (text)
            if (tokenLen >= 4) {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG);
            } else {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT);
            }
            break;
        case 'Z': // time zone (value)
            if (tokenLen == 1) {
                rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
            } else if (tokenLen == 2) {
                rule = TimeZoneNumberRule.INSTANCE_ISO_8601;
            } else {
                rule = TimeZoneNumberRule.INSTANCE_COLON;
            }
            break;
        case '\'': // literal text
            final String sub = token.substring(1);
            if (sub.length() == 1) {
                rule = new CharacterLiteral(sub.charAt(0));
            } else {
                rule = new StringLiteral(sub);
            }
            break;
        default:
            throw new IllegalArgumentException("Illegal pattern component: " + token);
        }

        rules.add(rule);
    }

    return rules;
}

From source file:org.apache.click.extras.control.DateField.java

/**
 * Return the names of months and weekdays as a script.
 *//* w  ww . j a  v  a 2  s  . com*/
protected void addCalenderTranslations(List<Element> headElements) {
    JsScript script = new JsScript();
    script.setId("datefield-js-setup-global");
    if (!headElements.contains(script)) {
        DateFormatSymbols dfs = new DateFormatSymbols(getLocale());

        HtmlStringBuffer buffer = new HtmlStringBuffer(150);
        buffer.append("Date.months=new Array(");
        generateJavaScriptArray(buffer, dfs.getMonths(), 0, 12);
        buffer.append(");\n");

        buffer.append("Date.monthAbbreviations=new Array(");
        generateJavaScriptArray(buffer, dfs.getShortMonths(), 0, 12);
        buffer.append(");\n");

        buffer.append("Date.dayNames=new Array(");
        generateJavaScriptArray(buffer, dfs.getWeekdays(), Calendar.SUNDAY, Calendar.SATURDAY + 1);
        buffer.append(");\n");

        buffer.append("Date.dayAbbreviations=new Array(");
        generateJavaScriptArray(buffer, dfs.getShortWeekdays(), Calendar.SUNDAY, Calendar.SATURDAY + 1);
        buffer.append(");\n");

        String[] weekdays = null;
        if (getMessages().containsKey("calendar-weekdays-heading")) {
            String headings = getMessage("calendar-weekdays-heading");
            weekdays = StringUtils.splitPreserveAllTokens("," + headings, ',');
        } else {
            weekdays = dfs.getShortWeekdays();
        }
        String[] days = new String[7];
        int firstDayOfWeek = getFirstDayOfWeek() - 1;
        for (int i = 0; i < 7; i++) {
            days[i] = weekdays[(i + firstDayOfWeek) % 7 + 1];
        }
        buffer.append("Date.weekdays=new Array(");
        generateJavaScriptArray(buffer, days, 0, 7);
        buffer.append(");\n");

        buffer.append("Date.first_day_of_week=").append(firstDayOfWeek).append(";\n");
        if (getMessages().containsKey("calendar-ok")) {
            buffer.append("_translations[\"OK\"] = \"");
            buffer.append(getMessage("calendar-ok"));
            buffer.append("\";\n");
        }
        if (getMessages().containsKey("calendar-now")) {
            buffer.append("_translations[\"Now\"] = \"");
            buffer.append(getMessage("calendar-now"));
            buffer.append("\";\n");
        }
        if (getMessages().containsKey("calendar-today")) {
            buffer.append("_translations[\"Today\"] = \"");
            buffer.append(getMessage("calendar-today"));
            buffer.append("\";\n");
        }
        if (getMessages().containsKey("calendar-clear")) {
            buffer.append("_translations[\"Clear\"] = \"");
            buffer.append(getMessage("calendar-clear"));
            buffer.append("\";\n");
        }

        script.setContent(buffer.toString());
        headElements.add(script);
    }
}

From source file:org.apache.logging.log4j.core.util.datetime.FastDatePrinter.java

/**
 * <p>Returns a list of Rules given a pattern.</p>
 *
 * @return a {@code List} of Rule objects
 * @throws IllegalArgumentException if pattern is invalid
 *///from  w w w . ja  v  a2s  .c o m
protected List<Rule> parsePattern() {
    final DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
    final List<Rule> rules = new ArrayList<>();

    final String[] ERAs = symbols.getEras();
    final String[] months = symbols.getMonths();
    final String[] shortMonths = symbols.getShortMonths();
    final String[] weekdays = symbols.getWeekdays();
    final String[] shortWeekdays = symbols.getShortWeekdays();
    final String[] AmPmStrings = symbols.getAmPmStrings();

    final int length = mPattern.length();
    final int[] indexRef = new int[1];

    for (int i = 0; i < length; i++) {
        indexRef[0] = i;
        final String token = parseToken(mPattern, indexRef);
        i = indexRef[0];

        final int tokenLen = token.length();
        if (tokenLen == 0) {
            break;
        }

        Rule rule;
        final char c = token.charAt(0);

        switch (c) {
        case 'G': // era designator (text)
            rule = new TextField(Calendar.ERA, ERAs);
            break;
        case 'y': // year (number)
        case 'Y': // week year
            if (tokenLen == 2) {
                rule = TwoDigitYearField.INSTANCE;
            } else {
                rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
            }
            if (c == 'Y') {
                rule = new WeekYear((NumberRule) rule);
            }
            break;
        case 'M': // month in year (text and number)
            if (tokenLen >= 4) {
                rule = new TextField(Calendar.MONTH, months);
            } else if (tokenLen == 3) {
                rule = new TextField(Calendar.MONTH, shortMonths);
            } else if (tokenLen == 2) {
                rule = TwoDigitMonthField.INSTANCE;
            } else {
                rule = UnpaddedMonthField.INSTANCE;
            }
            break;
        case 'd': // day in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
            break;
        case 'h': // hour in am/pm (number, 1..12)
            rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
            break;
        case 'H': // hour in day (number, 0..23)
            rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
            break;
        case 'm': // minute in hour (number)
            rule = selectNumberRule(Calendar.MINUTE, tokenLen);
            break;
        case 's': // second in minute (number)
            rule = selectNumberRule(Calendar.SECOND, tokenLen);
            break;
        case 'S': // millisecond (number)
            rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
            break;
        case 'E': // day in week (text)
            rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
            break;
        case 'u': // day in week (number)
            rule = new DayInWeekField(selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen));
            break;
        case 'D': // day in year (number)
            rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
            break;
        case 'F': // day of week in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
            break;
        case 'w': // week in year (number)
            rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
            break;
        case 'W': // week in month (number)
            rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
            break;
        case 'a': // am/pm marker (text)
            rule = new TextField(Calendar.AM_PM, AmPmStrings);
            break;
        case 'k': // hour in day (1..24)
            rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
            break;
        case 'K': // hour in am/pm (0..11)
            rule = selectNumberRule(Calendar.HOUR, tokenLen);
            break;
        case 'X': // ISO 8601 
            rule = Iso8601_Rule.getRule(tokenLen);
            break;
        case 'z': // time zone (text)
            if (tokenLen >= 4) {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG);
            } else {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT);
            }
            break;
        case 'Z': // time zone (value)
            if (tokenLen == 1) {
                rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
            } else if (tokenLen == 2) {
                rule = Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES;
            } else {
                rule = TimeZoneNumberRule.INSTANCE_COLON;
            }
            break;
        case '\'': // literal text
            final String sub = token.substring(1);
            if (sub.length() == 1) {
                rule = new CharacterLiteral(sub.charAt(0));
            } else {
                rule = new StringLiteral(sub);
            }
            break;
        default:
            throw new IllegalArgumentException("Illegal pattern component: " + token);
        }

        rules.add(rule);
    }

    return rules;
}

From source file:DateFormatUtils.java

/**
 * <p>Returns a list of Rules given a pattern.</p>
 * //ww  w .  j a v a 2s.c om
 * @return a <code>List</code> of Rule objects
 * @throws IllegalArgumentException if pattern is invalid
 */
protected List parsePattern() {
    DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
    List rules = new ArrayList();

    String[] ERAs = symbols.getEras();
    String[] months = symbols.getMonths();
    String[] shortMonths = symbols.getShortMonths();
    String[] weekdays = symbols.getWeekdays();
    String[] shortWeekdays = symbols.getShortWeekdays();
    String[] AmPmStrings = symbols.getAmPmStrings();

    int length = mPattern.length();
    int[] indexRef = new int[1];

    for (int i = 0; i < length; i++) {
        indexRef[0] = i;
        String token = parseToken(mPattern, indexRef);
        i = indexRef[0];

        int tokenLen = token.length();
        if (tokenLen == 0) {
            break;
        }

        Rule rule;
        char c = token.charAt(0);

        switch (c) {
        case 'G': // era designator (text)
            rule = new TextField(Calendar.ERA, ERAs);
            break;
        case 'y': // year (number)
            if (tokenLen >= 4) {
                rule = selectNumberRule(Calendar.YEAR, tokenLen);
            } else {
                rule = TwoDigitYearField.INSTANCE;
            }
            break;
        case 'M': // month in year (text and number)
            if (tokenLen >= 4) {
                rule = new TextField(Calendar.MONTH, months);
            } else if (tokenLen == 3) {
                rule = new TextField(Calendar.MONTH, shortMonths);
            } else if (tokenLen == 2) {
                rule = TwoDigitMonthField.INSTANCE;
            } else {
                rule = UnpaddedMonthField.INSTANCE;
            }
            break;
        case 'd': // day in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
            break;
        case 'h': // hour in am/pm (number, 1..12)
            rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
            break;
        case 'H': // hour in day (number, 0..23)
            rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
            break;
        case 'm': // minute in hour (number)
            rule = selectNumberRule(Calendar.MINUTE, tokenLen);
            break;
        case 's': // second in minute (number)
            rule = selectNumberRule(Calendar.SECOND, tokenLen);
            break;
        case 'S': // millisecond (number)
            rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
            break;
        case 'E': // day in week (text)
            rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
            break;
        case 'D': // day in year (number)
            rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
            break;
        case 'F': // day of week in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
            break;
        case 'w': // week in year (number)
            rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
            break;
        case 'W': // week in month (number)
            rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
            break;
        case 'a': // am/pm marker (text)
            rule = new TextField(Calendar.AM_PM, AmPmStrings);
            break;
        case 'k': // hour in day (1..24)
            rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
            break;
        case 'K': // hour in am/pm (0..11)
            rule = selectNumberRule(Calendar.HOUR, tokenLen);
            break;
        case 'z': // time zone (text)
            if (tokenLen >= 4) {
                rule = new TimeZoneNameRule(mTimeZone, mTimeZoneForced, mLocale, TimeZone.LONG);
            } else {
                rule = new TimeZoneNameRule(mTimeZone, mTimeZoneForced, mLocale, TimeZone.SHORT);
            }
            break;
        case 'Z': // time zone (value)
            if (tokenLen == 1) {
                rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
            } else {
                rule = TimeZoneNumberRule.INSTANCE_COLON;
            }
            break;
        case '\'': // literal text
            String sub = token.substring(1);
            if (sub.length() == 1) {
                rule = new CharacterLiteral(sub.charAt(0));
            } else {
                rule = new StringLiteral(sub);
            }
            break;
        default:
            throw new IllegalArgumentException("Illegal pattern component: " + token);
        }

        rules.add(rule);
    }

    return rules;
}