KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > ChineseDateFormat


1 /*********************************************************************
2  * Copyright (C) 2000-2006, International Business Machines Corporation and
3  * others. All Rights Reserved.
4  *********************************************************************
5  */

6 package com.ibm.icu.text;
7 import com.ibm.icu.util.*;
8 import com.ibm.icu.impl.Utility;
9 import java.text.FieldPosition JavaDoc;
10 import java.text.ParsePosition JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 /**
14  * A concrete {@link DateFormat} for {@link com.ibm.icu.util.ChineseCalendar}.
15  * This class handles a <code>ChineseCalendar</code>-specific field,
16  * <code>ChineseCalendar.IS_LEAP_MONTH</code>. It also redefines the
17  * handling of two fields, <code>ERA</code> and <code>YEAR</code>. The
18  * former is displayed numerically, instead of symbolically, since it is
19  * the numeric cycle number in <code>ChineseCalendar</code>. The latter is
20  * numeric, as before, but has no special 2-digit Y2K behavior.
21  *
22  * <p>With regard to <code>ChineseCalendar.IS_LEAP_MONTH</code>, this
23  * class handles parsing specially. If no string symbol is found at all,
24  * this is taken as equivalent to an <code>IS_LEAP_MONTH</code> value of
25  * zero. This allows formats to display a special string (e.g., "*") for
26  * leap months, but no string for normal months.
27  *
28  * <p>Summary of field changes vs. {@link SimpleDateFormat}:<pre>
29  * Symbol Meaning Presentation Example
30  * ------ ------- ------------ -------
31  * G cycle (Number) 78
32  * y year of cycle (1..60) (Number) 17
33  * l is leap month (Text) 4637
34  * </pre>
35  *
36  * @see com.ibm.icu.util.ChineseCalendar
37  * @see ChineseDateFormatSymbols
38  * @author Alan Liu
39  * @stable ICU 2.0
40  */

41 public class ChineseDateFormat extends SimpleDateFormat {
42     // Generated by serialver from JDK 1.4.1_01
43
static final long serialVersionUID = -4610300753104099899L;
44     
45     // TODO Finish the constructors
46

47     /**
48      * Construct a ChineseDateFormat from a date format pattern and locale
49      * @param pattern the pattern
50      * @param locale the locale
51      * @stable ICU 2.0
52      */

53    public ChineseDateFormat(String JavaDoc pattern, Locale JavaDoc locale) {
54        this(pattern, ULocale.forLocale(locale));
55     }
56
57     /**
58      * Construct a ChineseDateFormat from a date format pattern and locale
59      * @param pattern the pattern
60      * @param locale the locale
61      * @draft ICU 3.2
62      * @provisional This API might change or be removed in a future release.
63      */

64    public ChineseDateFormat(String JavaDoc pattern, ULocale locale) {
65        super(pattern, new ChineseDateFormatSymbols(locale),
66                new ChineseCalendar(TimeZone.getDefault(), locale), locale, true);
67     }
68
69 // NOTE: This API still exists; we just inherit it from SimpleDateFormat
70
// as of ICU 3.0
71
// /**
72
// * @stable ICU 2.0
73
// */
74
// protected String subFormat(char ch, int count, int beginOffset,
75
// FieldPosition pos, DateFormatSymbols formatData,
76
// Calendar cal) {
77
// switch (ch) {
78
// case 'G': // 'G' - ERA
79
// return zeroPaddingNumber(cal.get(Calendar.ERA), 1, 9);
80
// case 'l': // 'l' - IS_LEAP_MONTH
81
// {
82
// ChineseDateFormatSymbols symbols =
83
// (ChineseDateFormatSymbols) formatData;
84
// return symbols.getLeapMonth(cal.get(
85
// ChineseCalendar.IS_LEAP_MONTH));
86
// }
87
// default:
88
// return super.subFormat(ch, count, beginOffset, pos, formatData, cal);
89
// }
90
// }
91

92     /**
93      * @internal
94      * @deprecated This API is ICU internal only.
95      */

96     protected void subFormat(StringBuffer JavaDoc buf,
97                              char ch, int count, int beginOffset,
98                              FieldPosition JavaDoc pos,
99                              Calendar cal) {
100         switch (ch) {
101         case 'G': // 'G' - ERA
102
zeroPaddingNumber(buf, cal.get(Calendar.ERA), 1, 9);
103             break;
104         case 'l': // 'l' - IS_LEAP_MONTH
105
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         // TODO: add code to set FieldPosition for 'G' and 'l' fields. This
114
// is a DESIGN FLAW -- subclasses shouldn't have to duplicate the
115
// code that handles this at the end of SimpleDateFormat.subFormat.
116
// The logic should be moved up into SimpleDateFormat.format.
117
}
118
119     /**
120      * @stable ICU 2.0
121      */

122     protected int subParse(String JavaDoc 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         // Skip whitespace
129
start = Utility.skipWhitespace(text, start);
130
131         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(start);
132
133         switch (ch) {
134         case 'G': // 'G' - ERA
135
case 'y': // 'y' - YEAR, but without the 2-digit Y2K adjustment
136
{
137                 Number JavaDoc 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': // 'l' - IS_LEAP_MONTH
154
{
155                 ChineseDateFormatSymbols symbols =
156                     (ChineseDateFormatSymbols) getSymbols();
157                 int result = matchString(text, start, ChineseCalendar.IS_LEAP_MONTH,
158                                          symbols.isLeapMonth, cal);
159                 // Treat the absence of any matching string as setting
160
// IS_LEAP_MONTH to false.
161
if (result<0) {
162                     cal.set(ChineseCalendar.IS_LEAP_MONTH, 0);
163                     result = start;
164                 }
165                 return result;
166             }
167         default:
168             ///CLOVER:OFF
169
return 0; // This can never happen
170
///CLOVER:ON
171
}
172     }
173 }
174
Popular Tags