1 6 package com.ibm.icu.text; 7 import com.ibm.icu.util.*; 8 import com.ibm.icu.impl.Utility; 9 import java.text.FieldPosition ; 10 import java.text.ParsePosition ; 11 import java.util.Locale ; 12 13 41 public class ChineseDateFormat extends SimpleDateFormat { 42 static final long serialVersionUID = -4610300753104099899L; 44 45 47 53 public ChineseDateFormat(String pattern, Locale locale) { 54 this(pattern, ULocale.forLocale(locale)); 55 } 56 57 64 public ChineseDateFormat(String pattern, ULocale locale) { 65 super(pattern, new ChineseDateFormatSymbols(locale), 66 new ChineseCalendar(TimeZone.getDefault(), locale), locale, true); 67 } 68 69 92 96 protected void subFormat(StringBuffer buf, 97 char ch, int count, int beginOffset, 98 FieldPosition pos, 99 Calendar cal) { 100 switch (ch) { 101 case 'G': zeroPaddingNumber(buf, cal.get(Calendar.ERA), 1, 9); 103 break; 104 case 'l': buf.append(((ChineseDateFormatSymbols) getSymbols()). 106 getLeapMonth(cal.get(ChineseCalendar.IS_LEAP_MONTH))); 107 break; 108 default: 109 super.subFormat(buf, ch, count, beginOffset, pos, cal); 110 break; 111 } 112 113 } 118 119 122 protected int subParse(String text, int start, char ch, int count, 123 boolean obeyCount, boolean allowNegative, boolean[] ambiguousYear, Calendar cal) { 124 if (ch != 'G' && ch != 'l' && ch != 'y') { 125 return super.subParse(text, start, ch, count, obeyCount, allowNegative, ambiguousYear, cal); 126 } 127 128 start = Utility.skipWhitespace(text, start); 130 131 ParsePosition pos = new ParsePosition (start); 132 133 switch (ch) { 134 case 'G': case 'y': { 137 Number number = null; 138 if (obeyCount) { 139 if ((start+count) > text.length()) { 140 return -start; 141 } 142 number = numberFormat.parse(text.substring(0, start+count), pos); 143 } else { 144 number = numberFormat.parse(text, pos); 145 } 146 if (number == null) { 147 return -start; 148 } 149 int value = number.intValue(); 150 cal.set(ch == 'G' ? Calendar.ERA : Calendar.YEAR, value); 151 return pos.getIndex(); 152 } 153 case 'l': { 155 ChineseDateFormatSymbols symbols = 156 (ChineseDateFormatSymbols) getSymbols(); 157 int result = matchString(text, start, ChineseCalendar.IS_LEAP_MONTH, 158 symbols.isLeapMonth, cal); 159 if (result<0) { 162 cal.set(ChineseCalendar.IS_LEAP_MONTH, 0); 163 result = start; 164 } 165 return result; 166 } 167 default: 168 return 0; } 172 } 173 } 174 | Popular Tags |