1:
40:
41: package ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57:
58:
62: public class MonthDateFormat extends DateFormat {
63:
64:
65: private String[] months;
66:
67:
68: private boolean[] showYear;
69:
70:
71: private DateFormat yearFormatter;
72:
73:
76: public MonthDateFormat() {
77: this(TimeZone.getDefault());
78: }
79:
80:
85: public MonthDateFormat(TimeZone zone) {
86: this(zone, Locale.getDefault(), 1, true, false);
87: }
88:
89:
95: public MonthDateFormat(Locale locale) {
96: this(TimeZone.getDefault(), locale, 1, true, false);
97: }
98:
99:
108: public MonthDateFormat(TimeZone zone, int chars) {
109: this(zone, Locale.getDefault(), chars, true, false);
110: }
111:
112:
121: public MonthDateFormat(Locale locale, int chars) {
122: this(TimeZone.getDefault(), locale, chars, true, false);
123: }
124:
125:
142: public MonthDateFormat(TimeZone zone, Locale locale, int chars,
143: boolean showYearForJan, boolean showYearForDec) {
144: this(zone, locale, chars, new boolean[] {showYearForJan, false, false,
145: false, false, false, false, false, false, false, false, false,
146: showYearForDec}, new SimpleDateFormat("yy"));
147: }
148:
149:
163: public MonthDateFormat(TimeZone zone, Locale locale, int chars,
164: boolean[] showYear, DateFormat yearFormatter) {
165: if (locale == null) {
166: throw new IllegalArgumentException("Null 'locale' argument.");
167: }
168: DateFormatSymbols dfs = new DateFormatSymbols(locale);
169: String[] monthsFromLocale = dfs.getMonths();
170: this.months = new String[12];
171: for (int i = 0; i < 12; i++) {
172: if (chars > 0) {
173: this.months[i] = monthsFromLocale[i].substring(0,
174: Math.min(chars, monthsFromLocale[i].length()));
175: }
176: else {
177: this.months[i] = monthsFromLocale[i];
178: }
179: }
180: this.calendar = new GregorianCalendar(zone);
181: this.showYear = showYear;
182: this.yearFormatter = yearFormatter;
183:
184:
185:
186:
187: this.numberFormat = NumberFormat.getNumberInstance();
188: }
189:
190:
199: public StringBuffer format(Date date, StringBuffer toAppendTo,
200: FieldPosition fieldPosition) {
201: this.calendar.setTime(date);
202: int month = this.calendar.get(Calendar.MONTH);
203: toAppendTo.append(this.months[month]);
204: if (this.showYear[month]) {
205: toAppendTo.append(this.yearFormatter.format(date));
206: }
207: return toAppendTo;
208: }
209:
210:
218: public Date parse(String source, ParsePosition pos) {
219: return null;
220: }
221:
222:
229: public boolean equals(Object obj) {
230: if (obj == this) {
231: return true;
232: }
233: if (!(obj instanceof MonthDateFormat)) {
234: return false;
235: }
236: if (!super.equals(obj)) {
237: return false;
238: }
239: MonthDateFormat that = (MonthDateFormat) obj;
240: if (!Arrays.equals(this.months, that.months)) {
241: return false;
242: }
243: if (!Arrays.equals(this.showYear, that.showYear)) {
244: return false;
245: }
246: if (!this.yearFormatter.equals(that.yearFormatter)) {
247: return false;
248: }
249: return true;
250: }
251:
252:
257: public static void main(String[] args) {
258: MonthDateFormat mdf = new MonthDateFormat(Locale.UK, 2);
259: System.out.println("UK:");
260: System.out.println(mdf.format(new Month(1, 2005).getStart()));
261: System.out.println(mdf.format(new Month(2, 2005).getStart()));
262: System.out.println(mdf.format(new Month(3, 2005).getStart()));
263: System.out.println(mdf.format(new Month(4, 2005).getStart()));
264: System.out.println(mdf.format(new Month(5, 2005).getStart()));
265: System.out.println(mdf.format(new Month(6, 2005).getStart()));
266: System.out.println(mdf.format(new Month(7, 2005).getStart()));
267: System.out.println(mdf.format(new Month(8, 2005).getStart()));
268: System.out.println(mdf.format(new Month(9, 2005).getStart()));
269: System.out.println(mdf.format(new Month(10, 2005).getStart()));
270: System.out.println(mdf.format(new Month(11, 2005).getStart()));
271: System.out.println(mdf.format(new Month(12, 2005).getStart()));
272: System.out.println();
273:
274: mdf = new MonthDateFormat(Locale.GERMANY, 2);
275: System.out.println("GERMANY:");
276: System.out.println(mdf.format(new Month(1, 2005).getStart()));
277: System.out.println(mdf.format(new Month(2, 2005).getStart()));
278: System.out.println(mdf.format(new Month(3, 2005).getStart()));
279: System.out.println(mdf.format(new Month(4, 2005).getStart()));
280: System.out.println(mdf.format(new Month(5, 2005).getStart()));
281: System.out.println(mdf.format(new Month(6, 2005).getStart()));
282: System.out.println(mdf.format(new Month(7, 2005).getStart()));
283: System.out.println(mdf.format(new Month(8, 2005).getStart()));
284: System.out.println(mdf.format(new Month(9, 2005).getStart()));
285: System.out.println(mdf.format(new Month(10, 2005).getStart()));
286: System.out.println(mdf.format(new Month(11, 2005).getStart()));
287: System.out.println(mdf.format(new Month(12, 2005).getStart()));
288: System.out.println();
289:
290: mdf = new MonthDateFormat(Locale.FRANCE, 2);
291: System.out.println("FRANCE:");
292: System.out.println(mdf.format(new Month(1, 2005).getStart()));
293: System.out.println(mdf.format(new Month(2, 2005).getStart()));
294: System.out.println(mdf.format(new Month(3, 2005).getStart()));
295: System.out.println(mdf.format(new Month(4, 2005).getStart()));
296: System.out.println(mdf.format(new Month(5, 2005).getStart()));
297: System.out.println(mdf.format(new Month(6, 2005).getStart()));
298: System.out.println(mdf.format(new Month(7, 2005).getStart()));
299: System.out.println(mdf.format(new Month(8, 2005).getStart()));
300: System.out.println(mdf.format(new Month(9, 2005).getStart()));
301: System.out.println(mdf.format(new Month(10, 2005).getStart()));
302: System.out.println(mdf.format(new Month(11, 2005).getStart()));
303: System.out.println(mdf.format(new Month(12, 2005).getStart()));
304: System.out.println();
305:
306: SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
307: sdf.setNumberFormat(null);
308: }
309: }