/* Initialize the fields we use to disambiguate ambiguous years. Separate * so we can call it from readObject(). */ privatevoidinitializeDefaultCentury(){ calendar.setTimeInMillis(System.currentTimeMillis()); //注意此处将当前年年份减去80年 calendar.add( Calendar.YEAR, -80 ); parseAmbiguousDatesAsAfter(calendar.getTime()); }
/* Define one-century window into which to disambiguate dates using * two-digit years. */ privatevoidparseAmbiguousDatesAsAfter(Date startDate){ defaultCenturyStart = startDate; calendar.setTime(startDate); //此处年份已经比当前少了80年 defaultCenturyStartYear = calendar.get(Calendar.YEAR); }
/** * Private member function that converts the parsed date strings into * timeFields. Returns -start (for ParsePosition) if failed. * @param text the time text to be parsed. * @param start where to start parsing. * @param patternCharIndex the index of the pattern character. * @param count the count of a pattern character. * @param obeyCount if true, then the next field directly abuts this one, * and we should use the count to know when to stop parsing. * @param ambiguousYear return parameter; upon return, if ambiguousYear[0] * is true, then a two-digit year was parsed and may need to be readjusted. * @param origPos origPos.errorIndex is used to return an error index * at which a parse error occurred, if matching failure occurs. * @return the new start position if matching succeeded; -1 indicating * matching failure, otherwise. In case matching failure occurred, * an error index is set to origPos.errorIndex. */ privateintsubParse(String text, int start, int patternCharIndex, int count, boolean obeyCount, boolean[] ambiguousYear, ParsePosition origPos, boolean useFollowingMinusSignAsDelimiter, CalendarBuilder calb){ Number number; int value = 0; ParsePosition pos = new ParsePosition(0); pos.index = start; ......
parsing: { ...... case PATTERN_YEAR: // 'y' ......
// If there are 3 or more YEAR pattern characters, this indicates // that the year value is to be treated literally, without any // two-digit year adjustments (e.g., from "01" to 2001). Otherwise // we made adjustments to place the 2-digit year in the proper // century, for parsed strings from "00" to "99". Any other string // is treated literally: "2250", "-1", "1", "002". if (count <= 2 && (pos.index - actualStart) == 2 && Character.isDigit(text.charAt(actualStart)) && Character.isDigit(text.charAt(actualStart + 1))) { // Assume for example that the defaultCenturyStart is 6/18/1903. // This means that two-digit years will be forced into the range // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02 // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond // to 1904, 1905, etc. If the year is 03, then it is 2003 if the // other fields specify a date before 6/18, or 1903 if they specify a // date afterwards. As a result, 03 is an ambiguous year. All other // two-digit years are unambiguous. int ambiguousTwoDigitYear = defaultCenturyStartYear % 100; ambiguousYear[0] = value == ambiguousTwoDigitYear; value += (defaultCenturyStartYear/100)*100 + (value < ambiguousTwoDigitYear ? 100 : 0); } calb.set(field, value); return pos.index;
/** * Sets the 100-year period 2-digit years will be interpreted as being in * to begin on the date the user specifies. * * @param startDate During parsing, two digit years will be placed in the range * startDate to startDate + 100 years. * @see #get2DigitYearStart * @since 1.2 */ publicvoidset2DigitYearStart(Date startDate){ parseAmbiguousDatesAsAfter(new Date(startDate.getTime())); }
共有 0 条评论