Example usage for java.text DateFormatSymbols getEras

List of usage examples for java.text DateFormatSymbols getEras

Introduction

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

Prototype

public String[] getEras() 

Source Link

Document

Gets era strings.

Usage

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

private static String[] getDisplayNameArray(int field, boolean isLong, Locale locale) {
    DateFormatSymbols dfs = new DateFormatSymbols(locale);
    switch (field) {
    case Calendar.AM_PM:
        return dfs.getAmPmStrings();
    case Calendar.DAY_OF_WEEK:
        return isLong ? dfs.getWeekdays() : dfs.getShortWeekdays();
    case Calendar.ERA:
        return dfs.getEras();
    case Calendar.MONTH:
        return isLong ? dfs.getMonths() : dfs.getShortMonths();
    }//from   w w  w.  ja  v a  2s  .c  om
    return null;
}

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 w  ww.  jav a 2s  . c om*/
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:DateFormatUtils.java

/**
 * <p>Returns a list of Rules given a pattern.</p>
 * /*  w  w  w  .  j  a  va2  s.  c  o  m*/
 * @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;
}

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
 *//*  ww w  .  ja  va2s  .com*/
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:org.apache.myfaces.custom.calendar.HtmlCalendarRenderer.java

/**
 * Creates and returns a String which contains the initialisation data for
 * the popup calendar control as a sequence of javascript commands that
 * assign values to properties of a javascript object whose name is in
 * parameter popupCalendarVariable.//w w  w.  ja v a2 s .  c  o  m
 * <p>
 * 
 * @param firstDayOfWeek
 *            is in java.util.Calendar form, ie Sun=1, Mon=2, Sat=7
 */
public static String getLocalizedLanguageScript(FacesContext facesContext, DateFormatSymbols symbols,
        int firstDayOfWeek, UIComponent uiComponent, String popupCalendarVariable) {

    // Convert day value to java.util.Date convention (Sun=0, Mon=1, Sat=6).
    // This is the convention that javascript Date objects use.
    int realFirstDayOfWeek = firstDayOfWeek - 1;

    String[] weekDays;

    if (realFirstDayOfWeek == 0) {
        // Sunday
        weekDays = mapShortWeekdaysStartingWithSunday(symbols);
    } else if (realFirstDayOfWeek == 1) {
        // Monday
        weekDays = mapShortWeekdays(symbols);
    } else if (realFirstDayOfWeek == 6) {
        // Saturday. Often used in Arabic countries
        weekDays = mapShortWeekdaysStartingWithSaturday(symbols);
    } else {
        throw new IllegalStateException("Week may only start with saturday, sunday or monday.");
    }

    StringBuffer script = new StringBuffer();
    AddResource ar = AddResourceFactory.getInstance(facesContext);

    if (uiComponent instanceof HtmlInputCalendar) {
        HtmlInputCalendar calendar = (HtmlInputCalendar) uiComponent;
        // Set the themePrefix variable
        String popupTheme = calendar.getPopupTheme();
        if (popupTheme == null) {
            popupTheme = "DB";
        }
        setStringVariable(script, popupCalendarVariable + ".initData.themePrefix", "jscalendar-" + popupTheme);

        // specify the URL for the directory in which all the .gif images
        // can be found
        String imageLocation = HtmlRendererUtils.getImageLocation(uiComponent);
        if (imageLocation == null) {
            String imageLibrary = (String) uiComponent.getAttributes()
                    .get(LibraryLocationAware.IMAGE_LIBRARY_ATTR);
            if (imageLibrary == null) {
                //String uri = ar.getResourceUri(facesContext, HtmlCalendarRenderer.class, popupTheme
                //        + "/");
                //setStringVariable(script, popupCalendarVariable + ".initData.imgDir",
                //        JavascriptUtils.encodeString(uri));
                Resource resource = facesContext.getApplication().getResourceHandler().createResource(";j",
                        "oam.custom.calendar"
                                + ((popupTheme != null && popupTheme.length() > 0) ? "." + popupTheme : ""));
                String path = resource.getRequestPath();
                int index = path.indexOf("/;j");
                String prefix = path.substring(0, index + 1);
                String suffix = path.substring(index + 3);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDir", prefix);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix", suffix);
            } else {
                Resource resource = facesContext.getApplication().getResourceHandler().createResource(";j",
                        imageLibrary);
                String path = resource.getRequestPath();
                int index = path.indexOf("/;j");
                String prefix = path.substring(0, index + 1);
                String suffix = path.substring(index + 3);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDir", prefix);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix", suffix);
            }
        } else {
            setStringVariable(script, popupCalendarVariable + ".initData.imgDir",
                    (JavascriptUtils.encodeString(AddResourceFactory.getInstance(facesContext)
                            .getResourceUri(facesContext, imageLocation + "/"))));
            setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix", "");
        }
    } else {
        String imageLocation = HtmlRendererUtils.getImageLocation(uiComponent);
        if (imageLocation == null) {
            String imageLibrary = (String) uiComponent.getAttributes()
                    .get(LibraryLocationAware.IMAGE_LIBRARY_ATTR);
            if (imageLibrary == null) {
                //String uri = ar.getResourceUri(facesContext, HtmlCalendarRenderer.class, "images/");
                //setStringVariable(script, popupCalendarVariable + ".initData.imgDir",
                ///        JavascriptUtils.encodeString(uri));
                //setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix","");
                Resource resource = facesContext.getApplication().getResourceHandler().createResource(";j",
                        "oam.custom.calendar.images");
                String path = resource.getRequestPath();
                int index = path.indexOf("/;j");
                String prefix = path.substring(0, index + 1);
                String suffix = path.substring(index + 3);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDir", prefix);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix", suffix);
            } else {
                Resource resource = facesContext.getApplication().getResourceHandler().createResource(";j",
                        imageLibrary);
                String path = resource.getRequestPath();
                int index = path.indexOf("/;j");
                String prefix = path.substring(0, index + 1);
                String suffix = path.substring(index + 3);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDir", prefix);
                setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix", suffix);
            }
        } else {
            setStringVariable(script, popupCalendarVariable + ".initData.imgDir",
                    (JavascriptUtils.encodeString(AddResourceFactory.getInstance(facesContext)
                            .getResourceUri(facesContext, imageLocation + "/"))));
            setStringVariable(script, popupCalendarVariable + ".initData.imgDirSuffix", "");
        }
    }
    defineStringArray(script, popupCalendarVariable + ".initData.monthName", mapMonths(symbols));
    defineStringArray(script, popupCalendarVariable + ".initData.dayName", weekDays);
    setIntegerVariable(script, popupCalendarVariable + ".initData.startAt", realFirstDayOfWeek);

    defineStringArray(script, popupCalendarVariable + ".dateFormatSymbols.weekdays",
            mapWeekdaysStartingWithSunday(symbols));
    defineStringArray(script, popupCalendarVariable + ".dateFormatSymbols.shortWeekdays",
            mapShortWeekdaysStartingWithSunday(symbols));
    defineStringArray(script, popupCalendarVariable + ".dateFormatSymbols.shortMonths",
            mapShortMonths(symbols));
    defineStringArray(script, popupCalendarVariable + ".dateFormatSymbols.months", mapMonths(symbols));
    defineStringArray(script, popupCalendarVariable + ".dateFormatSymbols.eras", symbols.getEras());
    defineStringArray(script, popupCalendarVariable + ".dateFormatSymbols.ampms", symbols.getAmPmStrings());

    if (uiComponent instanceof HtmlInputCalendar) {

        HtmlInputCalendar inputCalendar = (HtmlInputCalendar) uiComponent;

        if (inputCalendar.getPopupGotoString() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.gotoString",
                    inputCalendar.getPopupGotoString());
        if (inputCalendar.getPopupTodayString() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.todayString",
                    inputCalendar.getPopupTodayString());
        if (inputCalendar.getPopupTodayDateFormat() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.todayDateFormat",
                    inputCalendar.getPopupTodayDateFormat());
        else if (inputCalendar.getPopupDateFormat() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.todayDateFormat",
                    inputCalendar.getPopupDateFormat());
        if (inputCalendar.getPopupWeekString() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.weekString",
                    inputCalendar.getPopupWeekString());
        if (inputCalendar.getPopupScrollLeftMessage() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.scrollLeftMessage",
                    inputCalendar.getPopupScrollLeftMessage());
        if (inputCalendar.getPopupScrollRightMessage() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.scrollRightMessage",
                    inputCalendar.getPopupScrollRightMessage());
        if (inputCalendar.getPopupSelectMonthMessage() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.selectMonthMessage",
                    inputCalendar.getPopupSelectMonthMessage());
        if (inputCalendar.getPopupSelectYearMessage() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.selectYearMessage",
                    inputCalendar.getPopupSelectYearMessage());
        if (inputCalendar.getPopupSelectDateMessage() != null)
            setStringVariable(script, popupCalendarVariable + ".initData.selectDateMessage",
                    inputCalendar.getPopupSelectDateMessage());
        setBooleanVariable(script, popupCalendarVariable + ".initData.popupLeft", inputCalendar.isPopupLeft());

    }

    return script.toString();
}

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 w  w  w.  ja  v a2 s.  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 '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;
}