01: package com.projity.timescale;
02:
03: import java.text.DateFormat;
04: import java.text.DateFormatSymbols;
05: import java.text.FieldPosition;
06: import java.text.ParsePosition;
07: import java.text.SimpleDateFormat;
08: import java.util.Calendar;
09: import java.util.Date;
10: import java.util.Locale;
11:
12: import com.projity.strings.Messages;
13:
14: public class ExtendedDateFormat extends SimpleDateFormat {
15: protected boolean quarter, half, normal;
16: protected String text;
17:
18: public ExtendedDateFormat() {
19: super ();
20: }
21:
22: public ExtendedDateFormat(String pattern,
23: DateFormatSymbols formatSymbols) {
24: super (pattern, formatSymbols);
25: }
26:
27: public ExtendedDateFormat(String pattern, Locale locale) {
28: super (pattern, locale);
29: }
30:
31: public ExtendedDateFormat(String pattern) {
32: super (pattern);
33: }
34:
35: @Override
36: public void applyPattern(String pattern) {
37: quarter = false;
38: half = false;
39: normal = false;
40: if (pattern.startsWith("Q")) {
41: int l = pattern.startsWith("QQ") ? 2 : 1;
42: text = Messages.getString("Date.Quarter" + l);
43: if (l == 2)
44: text += " ";
45: pattern = pattern.substring(l);
46: quarter = true;
47: } else if (pattern.startsWith("L")) {
48: int l = pattern.startsWith("LL") ? 2 : 1;
49: text = Messages.getString("Date.Half" + l);
50: if (l == 2)
51: text += " ";
52: pattern = pattern.substring(l);
53: half = true;
54: }
55: if (pattern.length() > 0) {
56: super .applyPattern(pattern);
57: normal = true;
58: }
59: }
60:
61: @Override
62: public StringBuffer format(Date date, StringBuffer toAppendTo,
63: FieldPosition pos) {
64: if (quarter || half) {
65: calendar.setTime(date);
66: int month = calendar.get(Calendar.MONTH);
67: toAppendTo.append(text).append(
68: month / (quarter ? 3 : 6) + 1);
69: }
70: if (normal)
71: super.format(date, toAppendTo, pos);
72: return toAppendTo;
73: }
74:
75: }
|