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

📄 simpledateformat.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                    return newStart;
                else // count == 4 failed, now try count == 3
                    return matchString(text, start, Calendar.MONTH,
                                       formatData.shortMonths);
            }
        case 4: // 'k' - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
            // [We computed 'value' above.]
            if (value == calendar.getMaximum(Calendar.HOUR_OF_DAY)+1) value = 0;
            calendar.set(Calendar.HOUR_OF_DAY, value);
            return pos.index;
        case 9: { // 'E' - DAY_OF_WEEK
            // Want to be able to parse both short and long forms.
            // Try count == 4 (DDDD) first:
            int newStart = 0;
            if ((newStart=matchString(text, start, Calendar.DAY_OF_WEEK,
                                      formatData.weekdays)) > 0)
                return newStart;
            else // DDDD failed, now try DDD
                return matchString(text, start, Calendar.DAY_OF_WEEK,
                                   formatData.shortWeekdays);
        }
        case 14:    // 'a' - AM_PM
            return matchString(text, start, Calendar.AM_PM, formatData.ampms);
        case 15: // 'h' - HOUR:1-based.  eg, 11PM + 1 hour =>> 12 AM
            // [We computed 'value' above.]
            if (value == calendar.getLeastMaximum(Calendar.HOUR)+1) value = 0;
            calendar.set(Calendar.HOUR, value);
            return pos.index;
        case 17: // 'z' - ZONE_OFFSET
            // First try to parse generic forms such as GMT-07:00. Do this first
            // in case localized DateFormatZoneData contains the string "GMT"
            // for a zone; in that case, we don't want to match the first three
            // characters of GMT+/-HH:MM etc.
            {
                int sign = 0;
                int offset;

                // For time zones that have no known names, look for strings
                // of the form:
                //    GMT[+-]hours:minutes or
                //    GMT[+-]hhmm or
                //    GMT.
                if (text.regionMatches(true,start, GMT, 0, GMT.length()))
                {
                    calendar.set(Calendar.DST_OFFSET, 0);

                    pos.index = start + GMT.length();

                    if (pos.index == text.length()) {
                          calendar.set(Calendar.ZONE_OFFSET, 0 );
                          return pos.index;
                    }
                    else if( text.charAt(pos.index) == '+' )
                        sign = 1;
                    else if( text.charAt(pos.index) == '-' )
                        sign = -1;
                    else {
                        calendar.set(Calendar.ZONE_OFFSET, 0 );
                        return pos.index;
                    }

                    // Look for hours:minutes or hhmm.
                    pos.index++;
                    Number tzNumber = numberFormat.parse(text, pos);
                    if( tzNumber == null ) {
                        return -start;
                    }
                    if( text.charAt(pos.index) == ':' ) {
                        // This is the hours:minutes case
                        offset = tzNumber.intValue() * 60;
                        pos.index++;
                        tzNumber = numberFormat.parse(text, pos);
                        if( tzNumber == null ) {
                            return -start;
                        }
                        offset += tzNumber.intValue();
                    }
                    else {
                        // This is the hhmm case.
                        offset = tzNumber.intValue();
                        if( offset < 24 )
                            offset *= 60;
                        else
                            offset = offset % 100 + offset / 100 * 60;
                    }

                    // Fall through for final processing below of 'offset' and 'sign'.
                }
                else {
                    // At this point, check for named time zones by looking through
                    // the locale data from the DateFormatZoneData strings.
                    // Want to be able to parse both short and long forms.
                    for (i=0; i<formatData.zoneStrings.length; i++)
                    {
                        // Checking long and short zones [1 & 2],
                        // and long and short daylight [3 & 4].
                        int j = 1;
                        for (; j <= 4; ++j)
                        {
                            if (text.regionMatches(true, start,
                                                   formatData.zoneStrings[i][j], 0,
                                                   formatData.zoneStrings[i][j].length()))
                                break;
                        }
                        if (j <= 4)
                        {
                            TimeZone tz = TimeZone.getTimeZone(formatData.zoneStrings[i][0]);
                            calendar.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
                            // Must call set() with something -- TODO -- Fix this to
                            // use the correct DST SAVINGS for the zone.
                            calendar.set(Calendar.DST_OFFSET, j >= 3 ? millisPerHour : 0);
                            return (start + formatData.zoneStrings[i][j].length());
                        }
                    }

                    // As a last resort, look for numeric timezones of the form
                    // [+-]hhmm as specified by RFC 822.  This code is actually
                    // a little more permissive than RFC 822.  It will try to do
                    // its best with numbers that aren't strictly 4 digits long.
                    DecimalFormat fmt = new DecimalFormat("+####;-####");
                    fmt.setParseIntegerOnly(true);
                    Number tzNumber = fmt.parse( text, pos );
                    if( tzNumber == null ) {
                        return -start;   // Wasn't actually a number.
                    }
                    offset = tzNumber.intValue();
                    sign = 1;
                    if( offset < 0 ) {
                        sign = -1;
                        offset = -offset;
                    }
                    if( offset < 24 )
                        offset = offset * 60;
                    else
                        offset = offset % 100 + offset / 100 * 60;

                    // Fall through for final processing below of 'offset' and 'sign'.
                }

                // Do the final processing for both of the above cases.  We only
                // arrive here if the form GMT+/-... or an RFC 822 form was seen.
                if (sign != 0)
                {
                    offset *= millisPerMinute * sign;

                    if (calendar.getTimeZone().useDaylightTime())
                    {
                        calendar.set(Calendar.DST_OFFSET, millisPerHour);
                        offset -= millisPerHour;
                    }
                    calendar.set(Calendar.ZONE_OFFSET, offset);

                    return pos.index;
                }
            }

            // All efforts to parse a zone failed.
            return -start;

        default:
            // case 3: // 'd' - DATE
            // case 5: // 'H' - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
            // case 6: // 'm' - MINUTE
            // case 7: // 's' - SECOND
            // case 8: // 'S' - MILLISECOND
            // case 10: // 'D' - DAY_OF_YEAR
            // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
            // case 12: // 'w' - WEEK_OF_YEAR
            // case 13: // 'W' - WEEK_OF_MONTH
            // case 16: // 'K' - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM

            // Handle "generic" fields
            if (obeyCount)
            {
                if ((start+count) > text.length()) return -start;
                number = numberFormat.parse(text.substring(0, start+count), pos);
            }
            else number = numberFormat.parse(text, pos);
            if (number != null)
            {
                calendar.set(field, number.intValue());
                return pos.index;
            }
            return -start;
        }
    }


    /**
     * Translate a pattern, mapping each character in the from string to the
     * corresponding character in the to string.
     */
    private String translatePattern(String pattern, String from, String to) {
        StringBuffer result = new StringBuffer();
        boolean inQuote = false;
        for (int i = 0; i < pattern.length(); ++i) {
            char c = pattern.charAt(i);
            if (inQuote) {
                if (c == '\'')
                    inQuote = false;
            }
            else {
                if (c == '\'')
                    inQuote = true;
                else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    int ci = from.indexOf(c);
                    if (ci == -1)
                        throw new IllegalArgumentException("Illegal pattern " +
                                                           " character '" +
                                                           c + "'");
                    c = to.charAt(ci);
                }
            }
            result.append(c);
        }
        if (inQuote)
            throw new IllegalArgumentException("Unfinished quote in pattern");
        return result.toString();
    }

    /**
     * Return a pattern string describing this date format.
     */
    public String toPattern() {
        return pattern;
    }

    /**
     * Return a localized pattern string describing this date format.
     */
    public String toLocalizedPattern() {
        return translatePattern(pattern,
                                formatData.patternChars,
                                formatData.localPatternChars);
    }

    /**
     * Apply the given unlocalized pattern string to this date format.
     */
    public void applyPattern (String pattern)
    {
        this.pattern = pattern;
    }

    /**
     * Apply the given localized pattern string to this date format.
     */
    public void applyLocalizedPattern(String pattern) {
        this.pattern = translatePattern(pattern,
                                        formatData.localPatternChars,
                                        formatData.patternChars);
    }

    /**
     * Gets the date/time formatting data.
     * @return a copy of the date-time formatting data associated
     * with this date-time formatter.
     */
    public DateFormatSymbols getDateFormatSymbols()
    {
        return (DateFormatSymbols)formatData.clone();
    }

    /**
     * Allows you to set the date/time formatting data.
     * @param newFormatData the given date-time formatting data.
     */
    public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
    {
        this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
    }

    /**
     * Overrides Cloneable
     */
    public Object clone() {
        SimpleDateFormat other = (SimpleDateFormat) super.clone();
        other.formatData = (DateFormatSymbols) formatData.clone();
        return other;
    }

    /**
     * Override hashCode.
     * Generates the hash code for the SimpleDateFormat object
     */
    public int hashCode()
    {
        return pattern.hashCode();
        // just enough fields for a reasonable distribution
    }

    /**
     * Override equals.
     */
    public boolean equals(Object obj)
    {
        if (!super.equals(obj)) return false; // super does class check
        SimpleDateFormat that = (SimpleDateFormat) obj;
        return (pattern.equals(that.pattern)
                && formatData.equals(that.formatData));
    }

    /**
     * Override readObject.
     */
    private void readObject(ObjectInputStream stream)
         throws IOException, ClassNotFoundException {
             stream.defaultReadObject();
             if (serialVersionOnStream < 1) {
                 // didn't have defaultCenturyStart field
                 initializeDefaultCentury();
             }
             else {
                 // fill in dependent transient field
                 parseAmbiguousDatesAsAfter(defaultCenturyStart);
             }
             serialVersionOnStream = currentSerialVersion;
    }
}

⌨️ 快捷键说明

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