1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /********************************************************************* 4 * Copyright (C) 2000-2014, International Business Machines Corporation and 5 * others. All Rights Reserved. 6 ********************************************************************* 7 */ 8 package com.ibm.icu.text; 9 10 import java.io.InvalidObjectException; 11 import java.text.FieldPosition; 12 import java.util.Locale; 13 14 import com.ibm.icu.util.Calendar; 15 import com.ibm.icu.util.ChineseCalendar; 16 import com.ibm.icu.util.TimeZone; 17 import com.ibm.icu.util.ULocale; 18 19 /** 20 * A concrete {@link DateFormat} for {@link com.ibm.icu.util.ChineseCalendar}. 21 * This class handles a <code>ChineseCalendar</code>-specific field, 22 * <code>ChineseCalendar.IS_LEAP_MONTH</code>. It also redefines the 23 * handling of two fields, <code>ERA</code> and <code>YEAR</code>. The 24 * former is displayed numerically, instead of symbolically, since it is 25 * the numeric cycle number in <code>ChineseCalendar</code>. The latter is 26 * numeric, as before, but has no special 2-digit Y2K behavior. 27 * 28 * <p>With regard to <code>ChineseCalendar.IS_LEAP_MONTH</code>, this 29 * class handles parsing specially. If no string symbol is found at all, 30 * this is taken as equivalent to an <code>IS_LEAP_MONTH</code> value of 31 * zero. This allows formats to display a special string (e.g., "*") for 32 * leap months, but no string for normal months. 33 * 34 * <p>Summary of field changes vs. {@link SimpleDateFormat}:<pre> 35 * Symbol Meaning Presentation Example 36 * ------ ------- ------------ ------- 37 * G cycle (Number) 78 38 * y year of cycle (1..60) (Number) 17 39 * l is leap month (Text) 4637 40 * </pre> 41 * 42 * @see com.ibm.icu.util.ChineseCalendar 43 * @see ChineseDateFormatSymbols 44 * @author Alan Liu 45 * @deprecated ICU 50 Use SimpleDateFormat instead. 46 */ 47 @Deprecated 48 public class ChineseDateFormat extends SimpleDateFormat { 49 // Generated by serialver from JDK 1.4.1_01 50 static final long serialVersionUID = -4610300753104099899L; 51 52 // TODO Finish the constructors 53 54 /** 55 * Construct a ChineseDateFormat from a date format pattern and locale 56 * @param pattern the pattern 57 * @param locale the locale 58 * @deprecated ICU 50 59 */ 60 @Deprecated 61 public ChineseDateFormat(String pattern, Locale locale) { 62 this(pattern, ULocale.forLocale(locale)); 63 } 64 65 /** 66 * Construct a ChineseDateFormat from a date format pattern and locale 67 * @param pattern the pattern 68 * @param locale the locale 69 * @deprecated ICU 50 70 */ 71 @Deprecated 72 public ChineseDateFormat(String pattern, ULocale locale) { 73 this(pattern, null, locale); 74 } 75 76 /** 77 * Construct a ChineseDateFormat from a date format pattern, numbering system override and locale 78 * @param pattern the pattern 79 * @param override The override string. A numbering system override string can take one of the following forms: 80 * 1). If just a numbering system name is specified, it applies to all numeric fields in the date format pattern. 81 * 2). To specify an alternate numbering system on a field by field basis, use the field letters from the pattern 82 * followed by an = sign, followed by the numbering system name. For example, to specify that just the year 83 * be formatted using Hebrew digits, use the override "y=hebr". Multiple overrides can be specified in a single 84 * string by separating them with a semi-colon. For example, the override string "m=thai;y=deva" would format using 85 * Thai digits for the month and Devanagari digits for the year. 86 * @param locale the locale 87 * @deprecated ICU 50 88 */ 89 @Deprecated 90 public ChineseDateFormat(String pattern, String override, ULocale locale) { 91 super(pattern, new ChineseDateFormatSymbols(locale), 92 new ChineseCalendar(TimeZone.getDefault(), locale), locale, true, override); 93 } 94 95 // NOTE: This API still exists; we just inherit it from SimpleDateFormat 96 // as of ICU 3.0 97 // /** 98 // * @stable ICU 2.0 99 // */ 100 // protected String subFormat(char ch, int count, int beginOffset, 101 // FieldPosition pos, DateFormatSymbols formatData, 102 // Calendar cal) { 103 // switch (ch) { 104 // case 'G': // 'G' - ERA 105 // return zeroPaddingNumber(cal.get(Calendar.ERA), 1, 9); 106 // case 'l': // 'l' - IS_LEAP_MONTH 107 // { 108 // ChineseDateFormatSymbols symbols = 109 // (ChineseDateFormatSymbols) formatData; 110 // return symbols.getLeapMonth(cal.get( 111 // ChineseCalendar.IS_LEAP_MONTH)); 112 // } 113 // default: 114 // return super.subFormat(ch, count, beginOffset, pos, formatData, cal); 115 // } 116 // } 117 118 /** 119 * {@inheritDoc} 120 * @internal 121 * @deprecated This API is ICU internal only. 122 */ 123 @Override 124 @Deprecated 125 protected void subFormat(StringBuffer buf, 126 char ch, int count, int beginOffset, 127 int fieldNum, DisplayContext capitalizationContext, 128 FieldPosition pos, 129 Calendar cal) { 130 131 // Logic to handle 'G' for chinese calendar is moved into SimpleDateFormat, 132 // and obsolete pattern char 'l' is now ignored in SimpleDateFormat, so we 133 // just use its implementation 134 super.subFormat(buf, ch, count, beginOffset, fieldNum, capitalizationContext, pos, cal); 135 136 // The following is no longer an issue for this subclass... 137 // TODO: add code to set FieldPosition for 'G' and 'l' fields. This 138 // is a DESIGN FLAW -- subclasses shouldn't have to duplicate the 139 // code that handles this at the end of SimpleDateFormat.subFormat. 140 // The logic should be moved up into SimpleDateFormat.format. 141 } 142 143 /** 144 * {@inheritDoc} 145 * 146 * @deprecated ICU 50 147 */ 148 @Deprecated 149 @Override 150 protected int subParse(String text, int start, char ch, int count, boolean obeyCount, boolean allowNegative, 151 boolean[] ambiguousYear, Calendar cal) { 152 // Logic to handle numeric 'G' eras for chinese calendar, and to skip special 2-digit year 153 // handling for chinese calendar, is moved into SimpleDateFormat, so delete here. 154 // Obsolete pattern char 'l' is now ignored for parsing in SimpleDateFormat, no handling 155 // needed here. 156 // So just use SimpleDateFormat implementation for this. 157 // just use its implementation 158 return super.subParse(text, start, ch, count, obeyCount, allowNegative, ambiguousYear, cal); 159 } 160 161 /** 162 * {@inheritDoc} 163 * 164 * @deprecated ICU 50 165 */ 166 @Override 167 @Deprecated 168 protected DateFormat.Field patternCharToDateFormatField(char ch) { 169 // no longer any field corresponding to pattern char 'l' 170 return super.patternCharToDateFormatField(ch); 171 } 172 173 /** 174 * The instances of this inner class are used as attribute keys and values 175 * in AttributedCharacterIterator that 176 * ChineseDateFormat.formatToCharacterIterator() method returns. 177 * <p> 178 * There is no public constructor to this class, the only instances are the 179 * constants defined here. 180 * <p> 181 * @deprecated ICU 50 182 */ 183 @Deprecated 184 public static class Field extends DateFormat.Field { 185 186 private static final long serialVersionUID = -5102130532751400330L; 187 188 /** 189 * Constant identifying the leap month marker. 190 * @deprecated ICU 50 This field is only used by the deprecated ChineseDateFormat class. 191 */ 192 @Deprecated 193 public static final Field IS_LEAP_MONTH = new Field("is leap month", ChineseCalendar.IS_LEAP_MONTH); 194 195 /** 196 * Constructs a <code>ChineseDateFormat.Field</code> with the given name and 197 * the <code>ChineseCalendar</code> field which this attribute represents. 198 * Use -1 for <code>calendarField</code> if this field does not have a 199 * corresponding <code>ChineseCalendar</code> field. 200 * 201 * @param name Name of the attribute 202 * @param calendarField <code>Calendar</code> field constant 203 * 204 * @deprecated ICU 50 205 */ 206 @Deprecated 207 protected Field(String name, int calendarField) { 208 super(name, calendarField); 209 } 210 211 /** 212 * Returns the <code>Field</code> constant that corresponds to the <code> 213 * ChineseCalendar</code> field <code>calendarField</code>. If there is no 214 * corresponding <code>Field</code> is available, null is returned. 215 * 216 * @param calendarField <code>ChineseCalendar</code> field constant 217 * @return <code>Field</code> associated with the <code>calendarField</code>, 218 * or null if no associated <code>Field</code> is available. 219 * @throws IllegalArgumentException if <code>calendarField</code> is not 220 * a valid <code>Calendar</code> field constant. 221 * 222 * @deprecated ICU 50 223 */ 224 @Deprecated 225 public static DateFormat.Field ofCalendarField(int calendarField) { 226 // Should we remove the following, since there is no longer a specific 227 // date format field for leap month (since 'l' pattern char is obsolete)? 228 if (calendarField == ChineseCalendar.IS_LEAP_MONTH) { 229 return IS_LEAP_MONTH; 230 } 231 return DateFormat.Field.ofCalendarField(calendarField); 232 } 233 234 /** 235 * {@inheritDoc} 236 * 237 * @deprecated ICU 50 238 */ 239 @Override 240 @Deprecated 241 ///CLOVER:OFF 242 protected Object readResolve() throws InvalidObjectException { 243 if (this.getClass() != ChineseDateFormat.Field.class) { 244 throw new InvalidObjectException("A subclass of ChineseDateFormat.Field must implement readResolve."); 245 } 246 if (this.getName().equals(IS_LEAP_MONTH.getName())) { 247 return IS_LEAP_MONTH; 248 } else { 249 throw new InvalidObjectException("Unknown attribute name."); 250 } 251 } 252 ///CLOVER:ON 253 } 254 } 255