src/share/classes/java/text/SimpleDateFormat.java

Print this page




1828         pos.index = start;
1829         if (patternCharIndex == PATTERN_WEEK_YEAR && !calendar.isWeekDateSupported()) {
1830             // use calendar year 'y' instead
1831             patternCharIndex = PATTERN_YEAR;
1832         }
1833         int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
1834 
1835         // If there are any spaces here, skip over them.  If we hit the end
1836         // of the string, then fail.
1837         for (;;) {
1838             if (pos.index >= text.length()) {
1839                 origPos.errorIndex = start;
1840                 return -1;
1841             }
1842             char c = text.charAt(pos.index);
1843             if (c != ' ' && c != '\t') {
1844                 break;
1845             }
1846             ++pos.index;
1847         }


1848 
1849       parsing:
1850         {
1851             // We handle a few special cases here where we need to parse
1852             // a number value.  We handle further, more generic cases below.  We need
1853             // to handle some of them here because some fields require extra processing on
1854             // the parsed value.
1855             if (patternCharIndex == PATTERN_HOUR_OF_DAY1 ||
1856                 patternCharIndex == PATTERN_HOUR1 ||
1857                 (patternCharIndex == PATTERN_MONTH && count <= 2) ||
1858                 patternCharIndex == PATTERN_YEAR ||
1859                 patternCharIndex == PATTERN_WEEK_YEAR) {
1860                 // It would be good to unify this with the obeyCount logic below,
1861                 // but that's going to be difficult.
1862                 if (obeyCount) {
1863                     if ((start+count) > text.length()) {
1864                         break parsing;
1865                     }
1866                     number = numberFormat.parse(text.substring(0, start+count), pos);
1867                 } else {


1907                 if (!(calendar instanceof GregorianCalendar)) {
1908                     // calendar might have text representations for year values,
1909                     // such as "\u5143" in JapaneseImperialCalendar.
1910                     int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
1911                     Map<String, Integer> map = calendar.getDisplayNames(field, style, locale);
1912                     if (map != null) {
1913                         if ((index = matchString(text, start, field, map, calb)) > 0) {
1914                             return index;
1915                         }
1916                     }
1917                     calb.set(field, value);
1918                     return pos.index;
1919                 }
1920 
1921                 // If there are 3 or more YEAR pattern characters, this indicates
1922                 // that the year value is to be treated literally, without any
1923                 // two-digit year adjustments (e.g., from "01" to 2001).  Otherwise
1924                 // we made adjustments to place the 2-digit year in the proper
1925                 // century, for parsed strings from "00" to "99".  Any other string
1926                 // is treated literally:  "2250", "-1", "1", "002".
1927                 if (count <= 2 && (pos.index - start) == 2
1928                     && Character.isDigit(text.charAt(start))
1929                     && Character.isDigit(text.charAt(start+1))) {
1930                     // Assume for example that the defaultCenturyStart is 6/18/1903.
1931                     // This means that two-digit years will be forced into the range
1932                     // 6/18/1903 to 6/17/2003.  As a result, years 00, 01, and 02
1933                     // correspond to 2000, 2001, and 2002.  Years 04, 05, etc. correspond
1934                     // to 1904, 1905, etc.  If the year is 03, then it is 2003 if the
1935                     // other fields specify a date before 6/18, or 1903 if they specify a
1936                     // date afterwards.  As a result, 03 is an ambiguous year.  All other
1937                     // two-digit years are unambiguous.
1938                     int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
1939                     ambiguousYear[0] = value == ambiguousTwoDigitYear;
1940                     value += (defaultCenturyStartYear/100)*100 +
1941                         (value < ambiguousTwoDigitYear ? 100 : 0);
1942                 }
1943                 calb.set(field, value);
1944                 return pos.index;
1945 
1946             case PATTERN_MONTH: // 'M'
1947                 if (count <= 2) // i.e., M or MM.
1948                 {
1949                     // Don't want to parse the month if it is a string




1828         pos.index = start;
1829         if (patternCharIndex == PATTERN_WEEK_YEAR && !calendar.isWeekDateSupported()) {
1830             // use calendar year 'y' instead
1831             patternCharIndex = PATTERN_YEAR;
1832         }
1833         int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
1834 
1835         // If there are any spaces here, skip over them.  If we hit the end
1836         // of the string, then fail.
1837         for (;;) {
1838             if (pos.index >= text.length()) {
1839                 origPos.errorIndex = start;
1840                 return -1;
1841             }
1842             char c = text.charAt(pos.index);
1843             if (c != ' ' && c != '\t') {
1844                 break;
1845             }
1846             ++pos.index;
1847         }
1848         // Remember the actual start index
1849         int actualStart = pos.index;
1850 
1851       parsing:
1852         {
1853             // We handle a few special cases here where we need to parse
1854             // a number value.  We handle further, more generic cases below.  We need
1855             // to handle some of them here because some fields require extra processing on
1856             // the parsed value.
1857             if (patternCharIndex == PATTERN_HOUR_OF_DAY1 ||
1858                 patternCharIndex == PATTERN_HOUR1 ||
1859                 (patternCharIndex == PATTERN_MONTH && count <= 2) ||
1860                 patternCharIndex == PATTERN_YEAR ||
1861                 patternCharIndex == PATTERN_WEEK_YEAR) {
1862                 // It would be good to unify this with the obeyCount logic below,
1863                 // but that's going to be difficult.
1864                 if (obeyCount) {
1865                     if ((start+count) > text.length()) {
1866                         break parsing;
1867                     }
1868                     number = numberFormat.parse(text.substring(0, start+count), pos);
1869                 } else {


1909                 if (!(calendar instanceof GregorianCalendar)) {
1910                     // calendar might have text representations for year values,
1911                     // such as "\u5143" in JapaneseImperialCalendar.
1912                     int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
1913                     Map<String, Integer> map = calendar.getDisplayNames(field, style, locale);
1914                     if (map != null) {
1915                         if ((index = matchString(text, start, field, map, calb)) > 0) {
1916                             return index;
1917                         }
1918                     }
1919                     calb.set(field, value);
1920                     return pos.index;
1921                 }
1922 
1923                 // If there are 3 or more YEAR pattern characters, this indicates
1924                 // that the year value is to be treated literally, without any
1925                 // two-digit year adjustments (e.g., from "01" to 2001).  Otherwise
1926                 // we made adjustments to place the 2-digit year in the proper
1927                 // century, for parsed strings from "00" to "99".  Any other string
1928                 // is treated literally:  "2250", "-1", "1", "002".
1929                 if (count <= 2 && (pos.index - actualStart) == 2
1930                     && Character.isDigit(text.charAt(actualStart))
1931                     && Character.isDigit(text.charAt(actualStart + 1))) {
1932                     // Assume for example that the defaultCenturyStart is 6/18/1903.
1933                     // This means that two-digit years will be forced into the range
1934                     // 6/18/1903 to 6/17/2003.  As a result, years 00, 01, and 02
1935                     // correspond to 2000, 2001, and 2002.  Years 04, 05, etc. correspond
1936                     // to 1904, 1905, etc.  If the year is 03, then it is 2003 if the
1937                     // other fields specify a date before 6/18, or 1903 if they specify a
1938                     // date afterwards.  As a result, 03 is an ambiguous year.  All other
1939                     // two-digit years are unambiguous.
1940                     int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
1941                     ambiguousYear[0] = value == ambiguousTwoDigitYear;
1942                     value += (defaultCenturyStartYear/100)*100 +
1943                         (value < ambiguousTwoDigitYear ? 100 : 0);
1944                 }
1945                 calb.set(field, value);
1946                 return pos.index;
1947 
1948             case PATTERN_MONTH: // 'M'
1949                 if (count <= 2) // i.e., M or MM.
1950                 {
1951                     // Don't want to parse the month if it is a string