1:
42:
43: package ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59:
60:
64: public class MonthDateFormat extends DateFormat {
65:
66:
67: private String[] months;
68:
69:
70: private boolean[] showYear;
71:
72:
73: private DateFormat yearFormatter;
74:
75:
78: public MonthDateFormat() {
79: this(TimeZone.getDefault());
80: }
81:
82:
87: public MonthDateFormat(TimeZone zone) {
88: this(zone, Locale.getDefault(), 1, true, false);
89: }
90:
91:
97: public MonthDateFormat(Locale locale) {
98: this(TimeZone.getDefault(), locale, 1, true, false);
99: }
100:
101:
110: public MonthDateFormat(TimeZone zone, int chars) {
111: this(zone, Locale.getDefault(), chars, true, false);
112: }
113:
114:
123: public MonthDateFormat(Locale locale, int chars) {
124: this(TimeZone.getDefault(), locale, chars, true, false);
125: }
126:
127:
144: public MonthDateFormat(TimeZone zone, Locale locale, int chars,
145: boolean showYearForJan, boolean showYearForDec) {
146: this(
147: zone, locale, chars, new boolean[] {showYearForJan, false, false,
148: false, false, false, false, false, false, false, false, false,
149: showYearForDec}, new SimpleDateFormat("yy")
150: );
151: }
152:
153:
167: public MonthDateFormat(TimeZone zone, Locale locale, int chars,
168: boolean[] showYear, DateFormat yearFormatter) {
169: if (locale == null) {
170: throw new IllegalArgumentException("Null 'locale' argument.");
171: }
172: DateFormatSymbols dfs = new DateFormatSymbols(locale);
173: String[] monthsFromLocale = dfs.getMonths();
174: this.months = new String[12];
175: for (int i = 0; i < 12; i++) {
176: if (chars > 0) {
177: months[i] = monthsFromLocale[i].substring(
178: 0, Math.min(chars, monthsFromLocale[i].length())
179: );
180: }
181: else {
182: months[i] = monthsFromLocale[i];
183: }
184: }
185: this.calendar = new GregorianCalendar(zone);
186: this.showYear = showYear;
187: this.yearFormatter = yearFormatter;
188:
189:
190:
191:
192: this.numberFormat = NumberFormat.getNumberInstance();
193: }
194:
195:
204: public StringBuffer format(Date date, StringBuffer toAppendTo,
205: FieldPosition fieldPosition) {
206: this.calendar.setTime(date);
207: int month = this.calendar.get(Calendar.MONTH);
208: toAppendTo.append(this.months[month]);
209: if (this.showYear[month]) {
210: toAppendTo.append(this.yearFormatter.format(date));
211: }
212: return toAppendTo;
213: }
214:
215:
223: public Date parse(String source, ParsePosition pos) {
224: return null;
225: }
226:
227:
234: public boolean equals(Object obj) {
235: if (obj == this) {
236: return true;
237: }
238: if (!(obj instanceof MonthDateFormat)) {
239: return false;
240: }
241: if (!super.equals(obj)) {
242: return false;
243: }
244: MonthDateFormat that = (MonthDateFormat) obj;
245: if (!Arrays.equals(this.months, that.months)) {
246: return false;
247: }
248: if (!Arrays.equals(this.showYear, that.showYear)) {
249: return false;
250: }
251: if (!this.yearFormatter.equals(that.yearFormatter)) {
252: return false;
253: }
254: return true;
255: }
256:
257:
262: public static void main(String[] args) {
263: MonthDateFormat mdf = new MonthDateFormat(Locale.UK, 2);
264: System.out.println("UK:");
265: System.out.println(mdf.format(new Month(1, 2005).getStart()));
266: System.out.println(mdf.format(new Month(2, 2005).getStart()));
267: System.out.println(mdf.format(new Month(3, 2005).getStart()));
268: System.out.println(mdf.format(new Month(4, 2005).getStart()));
269: System.out.println(mdf.format(new Month(5, 2005).getStart()));
270: System.out.println(mdf.format(new Month(6, 2005).getStart()));
271: System.out.println(mdf.format(new Month(7, 2005).getStart()));
272: System.out.println(mdf.format(new Month(8, 2005).getStart()));
273: System.out.println(mdf.format(new Month(9, 2005).getStart()));
274: System.out.println(mdf.format(new Month(10, 2005).getStart()));
275: System.out.println(mdf.format(new Month(11, 2005).getStart()));
276: System.out.println(mdf.format(new Month(12, 2005).getStart()));
277: System.out.println();
278:
279: mdf = new MonthDateFormat(Locale.GERMANY, 2);
280: System.out.println("GERMANY:");
281: System.out.println(mdf.format(new Month(1, 2005).getStart()));
282: System.out.println(mdf.format(new Month(2, 2005).getStart()));
283: System.out.println(mdf.format(new Month(3, 2005).getStart()));
284: System.out.println(mdf.format(new Month(4, 2005).getStart()));
285: System.out.println(mdf.format(new Month(5, 2005).getStart()));
286: System.out.println(mdf.format(new Month(6, 2005).getStart()));
287: System.out.println(mdf.format(new Month(7, 2005).getStart()));
288: System.out.println(mdf.format(new Month(8, 2005).getStart()));
289: System.out.println(mdf.format(new Month(9, 2005).getStart()));
290: System.out.println(mdf.format(new Month(10, 2005).getStart()));
291: System.out.println(mdf.format(new Month(11, 2005).getStart()));
292: System.out.println(mdf.format(new Month(12, 2005).getStart()));
293: System.out.println();
294:
295: mdf = new MonthDateFormat(Locale.FRANCE, 2);
296: System.out.println("FRANCE:");
297: System.out.println(mdf.format(new Month(1, 2005).getStart()));
298: System.out.println(mdf.format(new Month(2, 2005).getStart()));
299: System.out.println(mdf.format(new Month(3, 2005).getStart()));
300: System.out.println(mdf.format(new Month(4, 2005).getStart()));
301: System.out.println(mdf.format(new Month(5, 2005).getStart()));
302: System.out.println(mdf.format(new Month(6, 2005).getStart()));
303: System.out.println(mdf.format(new Month(7, 2005).getStart()));
304: System.out.println(mdf.format(new Month(8, 2005).getStart()));
305: System.out.println(mdf.format(new Month(9, 2005).getStart()));
306: System.out.println(mdf.format(new Month(10, 2005).getStart()));
307: System.out.println(mdf.format(new Month(11, 2005).getStart()));
308: System.out.println(mdf.format(new Month(12, 2005).getStart()));
309: System.out.println();
310:
311: SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
312: sdf.setNumberFormat(null);
313: System.out.println(sdf.equals("X"));
314: }
315: }