⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fastdateformat.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                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 = new UnpaddedNumberField(Calendar.YEAR);                }                else {                    rule = new TwoDigitYearField();                }                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 = new TwoDigitMonthField();                }                else {                    rule = new UnpaddedMonthField();                }                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 TimeZoneRule(timeZone, locale, TimeZone.LONG);                }                else {                    rule = new TimeZoneRule(timeZone, locale, TimeZone.SHORT);                }                break;            case '\'': // literal text                String sub = token.substring(1);                if (sub.length() == 1) {                    rule = new CharacterLiteral(sub.charAt(0));                }                else {                    rule = new StringLiteral(new String(sub));                }                break;            default:                throw new IllegalArgumentException                    ("Illegal pattern component: " + token);            }            rules.add(rule);        }        return rules;    }    private static String parseToken(String pattern, int[] indexRef) {        StringBuffer buf = new StringBuffer();        int i = indexRef[0];        int length = pattern.length();        char c = pattern.charAt(i);        if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {            // Scan a run of the same character, which indicates a time            // pattern.            buf.append(c);            while (i + 1 < length) {                char peek = pattern.charAt(i + 1);                if (peek == c) {                    buf.append(c);                    i++;                }                else {                    break;                }            }        }        else {            // This will identify token as text.            buf.append('\'');            boolean inLiteral = false;            for (; i < length; i++) {                c = pattern.charAt(i);                if (c == '\'') {                    if (i + 1 < length && pattern.charAt(i + 1) == '\'') {                        // '' is treated as escaped '                        i++;                        buf.append(c);                    }                    else {                        inLiteral = !inLiteral;                    }                }                else if (!inLiteral &&                         (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {                    i--;                    break;                }                else {                    buf.append(c);                }            }        }        indexRef[0] = i;        return buf.toString();    }    private static NumberRule selectNumberRule(int field, int padding) {        switch (padding) {        case 1:            return new UnpaddedNumberField(field);        case 2:            return new TwoDigitNumberField(field);        default:            return new PaddedNumberField(field, padding);        }    }    private final String mPattern;    private final TimeZone mTimeZone;    private final Locale mLocale;    private final Rule[] mRules;    private final int mMaxLengthEstimate;    private FastDateFormat() {        this(getDefaultPattern(), null, null, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     */    private FastDateFormat(String pattern) throws IllegalArgumentException {        this(pattern, null, null, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param timeZone optional time zone, overrides time zone of formatted     * date     */    private FastDateFormat(String pattern, TimeZone timeZone)        throws IllegalArgumentException    {        this(pattern, timeZone, null, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param locale optional locale, overrides system locale     */    private FastDateFormat(String pattern, Locale locale)        throws IllegalArgumentException    {        this(pattern, null, locale, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param symbols optional date format symbols, overrides symbols for     * system locale     */    private FastDateFormat(String pattern, DateFormatSymbols symbols)        throws IllegalArgumentException    {        this(pattern, null, null, symbols);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     */    private FastDateFormat(String pattern, TimeZone timeZone, Locale locale)        throws IllegalArgumentException    {        this(pattern, timeZone, locale, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     * @param symbols optional date format symbols, overrides symbols for     * provided locale     */    private FastDateFormat(String pattern, TimeZone timeZone, Locale locale,                           DateFormatSymbols symbols)        throws IllegalArgumentException    {        if (locale == null) {            locale = Locale.getDefault();        }        mPattern = pattern;        mTimeZone = timeZone;        mLocale = locale;        if (symbols == null) {            symbols = new DateFormatSymbols(locale);        }        List rulesList = parse(pattern, timeZone, locale, symbols);        mRules = (Rule[])rulesList.toArray(new Rule[rulesList.size()]);        int len = 0;        for (int i=mRules.length; --i >= 0; ) {            len += mRules[i].estimateLength();        }        mMaxLengthEstimate = len;    }    public String format(Date date) {        Calendar c = new GregorianCalendar(cDefaultTimeZone);        c.setTime(date);        if (mTimeZone != null) {            c.setTimeZone(mTimeZone);        }        return applyRules(c, new StringBuffer(mMaxLengthEstimate)).toString();    }    public String format(Calendar calendar) {        return format(calendar, new StringBuffer(mMaxLengthEstimate))            .toString();    }    public StringBuffer format(Date date, StringBuffer buf) {        Calendar c = new GregorianCalendar(cDefaultTimeZone);        c.setTime(date);        if (mTimeZone != null) {            c.setTimeZone(mTimeZone);        }        return applyRules(c, buf);    }    public StringBuffer format(Calendar calendar, StringBuffer buf) {        if (mTimeZone != null) {            calendar = (Calendar)calendar.clone();            calendar.setTimeZone(mTimeZone);        }        return applyRules(calendar, buf);    }    private StringBuffer applyRules(Calendar calendar, StringBuffer buf) {        Rule[] rules = mRules;        int len = mRules.length;        for (int i=0; i<len; i++) {            rules[i].appendTo(buf, calendar);        }        return buf;    }    public String getPattern() {        return mPattern;    }    /**     * Returns the time zone used by this formatter, or null if time zone of     * formatted dates is used instead.     */    public TimeZone getTimeZone() {        return mTimeZone;    }    public Locale getLocale() {        return mLocale;    }    /**     * Returns an estimate for the maximum length date that this date     * formatter will produce. The actual formatted length will almost always     * be less than or equal to this amount.     */    public int getMaxLengthEstimate() {        return mMaxLengthEstimate;    }    private interface Rule {        int estimateLength();        void appendTo(StringBuffer buffer, Calendar calendar);    }    private interface NumberRule extends Rule {        void appendTo(StringBuffer buffer, int value);    }    private static class CharacterLiteral implements Rule {        private final char mValue;        CharacterLiteral(char value) {            mValue = value;        }        public int estimateLength() {            return 1;        }        public void appendTo(StringBuffer buffer, Calendar calendar) {            buffer.append(mValue);        }    }    private static class StringLiteral implements Rule {        private final String mValue;        StringLiteral(String value) {            mValue = value;        }        public int estimateLength() {            return mValue.length();        }        public void appendTo(StringBuffer buffer, Calendar calendar) {            buffer.append(mValue);        }    }    private static class TextField implements Rule {        private final int mField;        private final String[] mValues;        TextField(int field, String[] values) {            mField = field;            mValues = values;        }        public int estimateLength() {            int max = 0;            for (int i=mValues.length; --i >= 0; ) {                int len = mValues[i].length();                if (len > max) {                    max = len;                }            }            return max;        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -