< prev index next >

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

Print this page
rev 53363 : imported patch 8216969
   1 /*
   2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1172                 current = "";
1173             }
1174             break;
1175 
1176         case PATTERN_WEEK_YEAR: // 'Y'
1177         case PATTERN_YEAR:      // 'y'
1178             if (calendar instanceof GregorianCalendar) {
1179                 if (count != 2) {
1180                     zeroPaddingNumber(value, count, maxIntCount, buffer);
1181                 } else {
1182                     zeroPaddingNumber(value, 2, 2, buffer);
1183                 } // clip 1996 to 96
1184             } else {
1185                 if (current == null) {
1186                     zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
1187                                       maxIntCount, buffer);
1188                 }
1189             }
1190             break;
1191 
1192         case PATTERN_MONTH:            // 'M' (context seinsive)
1193             if (useDateFormatSymbols) {
1194                 String[] months;
1195                 if (count >= 4) {
1196                     months = formatData.getMonths();
1197                     current = months[value];
1198                 } else if (count == 3) {
1199                     months = formatData.getShortMonths();
1200                     current = months[value];
1201                 }
1202             } else {
1203                 if (count < 3) {
1204                     current = null;
1205                 } else if (forceStandaloneForm) {
1206                     current = calendar.getDisplayName(field, style | 0x8000, locale);
1207                     if (current == null) {
1208                         current = calendar.getDisplayName(field, style, locale);
1209                     }
1210                 }
1211             }
1212             if (current == null) {


2016                     // [We computed 'value' above.]
2017                     calb.set(Calendar.MONTH, value - 1);
2018                     return pos.index;
2019                 }
2020 
2021                 if (useDateFormatSymbols) {
2022                     // count >= 3 // i.e., MMM or MMMM
2023                     // Want to be able to parse both short and long forms.
2024                     // Try count == 4 first:
2025                     int newStart;
2026                     if ((newStart = matchString(text, start, Calendar.MONTH,
2027                                                 formatData.getMonths(), calb)) > 0) {
2028                         return newStart;
2029                     }
2030                     // count == 4 failed, now try count == 3
2031                     if ((index = matchString(text, start, Calendar.MONTH,
2032                                              formatData.getShortMonths(), calb)) > 0) {
2033                         return index;
2034                     }
2035                 } else {
2036                     Map<String, Integer> map = getDisplayNamesMap(field, locale);
2037                     if ((index = matchString(text, start, field, map, calb)) > 0) {
2038                         return index;
2039                     }
2040                 }
2041                 break parsing;
2042 
2043             case PATTERN_MONTH_STANDALONE: // 'L'
2044                 if (count <= 2) {
2045                     // Don't want to parse the month if it is a string
2046                     // while pattern uses numeric style: L or LL
2047                     //[we computed 'value' above.]
2048                     calb.set(Calendar.MONTH, value - 1);
2049                     return pos.index;
2050                 }
2051                 Map<String, Integer> maps = getDisplayNamesMap(field, locale);
2052                 if ((index = matchString(text, start, field, maps, calb)) > 0) {
2053                     return index;
2054                 }
2055                 break parsing;
2056 


2432         SimpleDateFormat that = (SimpleDateFormat) obj;
2433         return (pattern.equals(that.pattern)
2434                 && formatData.equals(that.formatData));
2435     }
2436 
2437     private static final int[] REST_OF_STYLES = {
2438         Calendar.SHORT_STANDALONE, Calendar.LONG_FORMAT, Calendar.LONG_STANDALONE,
2439     };
2440     private Map<String, Integer> getDisplayNamesMap(int field, Locale locale) {
2441         Map<String, Integer> map = calendar.getDisplayNames(field, Calendar.SHORT_FORMAT, locale);
2442         // Get all SHORT and LONG styles (avoid NARROW styles).
2443         for (int style : REST_OF_STYLES) {
2444             Map<String, Integer> m = calendar.getDisplayNames(field, style, locale);
2445             if (m != null) {
2446                 map.putAll(m);
2447             }
2448         }
2449         return map;
2450     }
2451 













2452     /**
2453      * After reading an object from the input stream, the format
2454      * pattern in the object is verified.
2455      *
2456      * @exception InvalidObjectException if the pattern is invalid
2457      */
2458     private void readObject(ObjectInputStream stream)
2459                          throws IOException, ClassNotFoundException {
2460         stream.defaultReadObject();
2461 
2462         try {
2463             compiledPattern = compile(pattern);
2464         } catch (Exception e) {
2465             throw new InvalidObjectException("invalid pattern");
2466         }
2467 
2468         if (serialVersionOnStream < 1) {
2469             // didn't have defaultCenturyStart field
2470             initializeDefaultCentury();
2471         }


   1 /*
   2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1172                 current = "";
1173             }
1174             break;
1175 
1176         case PATTERN_WEEK_YEAR: // 'Y'
1177         case PATTERN_YEAR:      // 'y'
1178             if (calendar instanceof GregorianCalendar) {
1179                 if (count != 2) {
1180                     zeroPaddingNumber(value, count, maxIntCount, buffer);
1181                 } else {
1182                     zeroPaddingNumber(value, 2, 2, buffer);
1183                 } // clip 1996 to 96
1184             } else {
1185                 if (current == null) {
1186                     zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
1187                                       maxIntCount, buffer);
1188                 }
1189             }
1190             break;
1191 
1192         case PATTERN_MONTH:            // 'M' (context sensitive)
1193             if (useDateFormatSymbols) {
1194                 String[] months;
1195                 if (count >= 4) {
1196                     months = formatData.getMonths();
1197                     current = months[value];
1198                 } else if (count == 3) {
1199                     months = formatData.getShortMonths();
1200                     current = months[value];
1201                 }
1202             } else {
1203                 if (count < 3) {
1204                     current = null;
1205                 } else if (forceStandaloneForm) {
1206                     current = calendar.getDisplayName(field, style | 0x8000, locale);
1207                     if (current == null) {
1208                         current = calendar.getDisplayName(field, style, locale);
1209                     }
1210                 }
1211             }
1212             if (current == null) {


2016                     // [We computed 'value' above.]
2017                     calb.set(Calendar.MONTH, value - 1);
2018                     return pos.index;
2019                 }
2020 
2021                 if (useDateFormatSymbols) {
2022                     // count >= 3 // i.e., MMM or MMMM
2023                     // Want to be able to parse both short and long forms.
2024                     // Try count == 4 first:
2025                     int newStart;
2026                     if ((newStart = matchString(text, start, Calendar.MONTH,
2027                                                 formatData.getMonths(), calb)) > 0) {
2028                         return newStart;
2029                     }
2030                     // count == 4 failed, now try count == 3
2031                     if ((index = matchString(text, start, Calendar.MONTH,
2032                                              formatData.getShortMonths(), calb)) > 0) {
2033                         return index;
2034                     }
2035                 } else {
2036                     Map<String, Integer> map = getDisplayContextNamesMap(field, locale);
2037                     if ((index = matchString(text, start, field, map, calb)) > 0) {
2038                         return index;
2039                     }
2040                 }
2041                 break parsing;
2042 
2043             case PATTERN_MONTH_STANDALONE: // 'L'
2044                 if (count <= 2) {
2045                     // Don't want to parse the month if it is a string
2046                     // while pattern uses numeric style: L or LL
2047                     //[we computed 'value' above.]
2048                     calb.set(Calendar.MONTH, value - 1);
2049                     return pos.index;
2050                 }
2051                 Map<String, Integer> maps = getDisplayNamesMap(field, locale);
2052                 if ((index = matchString(text, start, field, maps, calb)) > 0) {
2053                     return index;
2054                 }
2055                 break parsing;
2056 


2432         SimpleDateFormat that = (SimpleDateFormat) obj;
2433         return (pattern.equals(that.pattern)
2434                 && formatData.equals(that.formatData));
2435     }
2436 
2437     private static final int[] REST_OF_STYLES = {
2438         Calendar.SHORT_STANDALONE, Calendar.LONG_FORMAT, Calendar.LONG_STANDALONE,
2439     };
2440     private Map<String, Integer> getDisplayNamesMap(int field, Locale locale) {
2441         Map<String, Integer> map = calendar.getDisplayNames(field, Calendar.SHORT_FORMAT, locale);
2442         // Get all SHORT and LONG styles (avoid NARROW styles).
2443         for (int style : REST_OF_STYLES) {
2444             Map<String, Integer> m = calendar.getDisplayNames(field, style, locale);
2445             if (m != null) {
2446                 map.putAll(m);
2447             }
2448         }
2449         return map;
2450     }
2451 
2452     // for 'M' pattern only
2453     private Map<String, Integer> getDisplayContextNamesMap(int field, Locale locale) {
2454         Map<String, Integer> map = calendar.getDisplayNames(field,
2455             Calendar.SHORT_FORMAT | (forceStandaloneForm ? 0x8000 : 0), locale);
2456         // Get LONG styles
2457         Map<String, Integer> m = calendar.getDisplayNames(field,
2458             Calendar.LONG_FORMAT | (forceStandaloneForm ? 0x8000 : 0), locale);
2459         if (m != null) {
2460             map.putAll(m);
2461         }
2462         return map;
2463     }
2464 
2465     /**
2466      * After reading an object from the input stream, the format
2467      * pattern in the object is verified.
2468      *
2469      * @exception InvalidObjectException if the pattern is invalid
2470      */
2471     private void readObject(ObjectInputStream stream)
2472                          throws IOException, ClassNotFoundException {
2473         stream.defaultReadObject();
2474 
2475         try {
2476             compiledPattern = compile(pattern);
2477         } catch (Exception e) {
2478             throw new InvalidObjectException("invalid pattern");
2479         }
2480 
2481         if (serialVersionOnStream < 1) {
2482             // didn't have defaultCenturyStart field
2483             initializeDefaultCentury();
2484         }


< prev index next >