Debian Codegraph @

src/com/jidesoft/converter/MonthNameConverter.java

raw
1 /*
2 * @(#)MonthNameConverter.java 5/8/2006
3 *
4 * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.converter;
8
9 import java.text.DateFormat;
10 import java.text.ParseException;
11 import java.text.SimpleDateFormat;
12 import java.util.Calendar;
13 import java.util.Date;
14
15 /**
16 * Converter which converts int to month string and converts it back.
17 */
18 public class MonthNameConverter implements ObjectConverter {
19
20 /**
21 * Default ConverterContext for MonthConverter.
22 */
23 public static ConverterContext CONTEXT = new ConverterContext("MonthName");
24
25 /**
26 * 0 -> "1", 1 -> "2", ..., 11 -> "12"
27 */
28 public static final DateFormat CONCISE_FORMAT = new SimpleDateFormat("M");
29
30 /**
31 * 0 -> "01", 1 -> "02", ..., 11 -> "12"
32 */
33 public static final DateFormat SHORT_FORMAT = new SimpleDateFormat("MM");
34
35 /**
36 * 0 -> "Jan", 1 -> "Feb", ..., 11 -> "Dec"
37 */
38 public static final DateFormat MEDIUM_FORMAT = new SimpleDateFormat("MMM");
39
40 /**
41 * 0 -> "January", 1 -> "February", ..., 11 -> "December"
42 */
43 public static final DateFormat LONG_FORMAT = new SimpleDateFormat("MMMMM");
44
45 private DateFormat _defaultFormat = MEDIUM_FORMAT;
46
47 /**
48 * Creates a new CalendarConverter.
49 */
50 public MonthNameConverter() {
51 }
52
53 public String toString(Object object, ConverterContext context) {
54 if (object == null || !(object instanceof Integer)) {
55 return "";
56 }
57 else {
58 return _defaultFormat.format((getCalendarByMonth((Integer) object).getTime()));
59 }
60 }
61
62 static final Calendar CAL = Calendar.getInstance();
63
64 static {
65 CAL.set(Calendar.DAY_OF_MONTH, 1);
66 }
67
68 protected Calendar getCalendarByMonth(int month) {
69 CAL.set(Calendar.MONTH, month);
70 return CAL;
71 }
72
73 public boolean supportToString(Object object, ConverterContext context) {
74 return true;
75 }
76
77 public Object fromString(String string, ConverterContext context) {
78 Calendar calendar = Calendar.getInstance();
79 try {
80 Date time = _defaultFormat.parse(string);
81 calendar.setTime(time);
82 }
83 catch (ParseException e1) { // if current formatter doesn't work try those default ones.
84 try {
85 Date time = SHORT_FORMAT.parse(string);
86 calendar.setTime(time);
87 }
88 catch (ParseException e2) {
89 try {
90 Date time = MEDIUM_FORMAT.parse(string);
91 calendar.setTime(time);
92 }
93 catch (ParseException e3) {
94 try {
95 Date time = LONG_FORMAT.parse(string);
96 calendar.setTime(time);
97 }
98 catch (ParseException e4) {
99 try {
100 Date time = CONCISE_FORMAT.parse(string);
101 calendar.setTime(time);
102 }
103 catch (ParseException e5) {
104 return null; // nothing works just return null so that old value will be kept.
105 }
106 }
107 }
108 }
109 }
110 return calendar.get(Calendar.MONTH);
111 }
112
113 public boolean supportFromString(String string, ConverterContext context) {
114 return true;
115 }
116
117 /**
118 * Gets default format to format a month.
119 *
120 * @return DefaultFormat
121 */
122 public DateFormat getDefaultFormat() {
123 return _defaultFormat;
124 }
125
126 /**
127 * Sets default format to format a month. Default is {@link #MEDIUM_FORMAT}.
128 *
129 * @param defaultFormat the default format to format the month.
130 */
131 public void setDefaultFormat(DateFormat defaultFormat) {
132 _defaultFormat = defaultFormat;
133 }
134
135 public static void main(String[] args) {
136 MonthNameConverter converter = new MonthNameConverter();
137 converter.setDefaultFormat(MonthNameConverter.LONG_FORMAT);
138 for (int i = 0; i < 12; i++) {
139 String str = converter.toString(new Integer(i), null);
140 System.out.println(str);
141 System.out.println(converter.fromString(str, null));
142 }
143 }
144 }