1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class DateFormatTool
49 {
50 /*** The date formater. */
51 protected DateFormatter dateFormater;
52
53 /*** The current locale. */
54 protected Locale locale;
55
56 /*** The DateFormat in use. */
57 protected DateFormat format;
58
59 /***
60 * Default constructor.
61 *
62 * @param dateFormater the date formatter object.
63 * @param locale the locale.
64 * @param format the date format object.
65 */
66 public DateFormatTool(DateFormatter dateFormater, Locale locale, DateFormat format)
67 {
68 this.dateFormater = dateFormater;
69 this.locale = locale;
70 this.format = format;
71 }
72
73
74
75 /***
76 * Sets the formatting style.
77 *
78 * @param patternAlias the pattern name.
79 * @return new DateFormatTool instance.
80 * @throws Exception if the specified pattern alias is not configured for this tool.
81 */
82 public DateFormatTool style(String patternAlias)
83 throws Exception
84 {
85 DateFormatTool target = createInstance(this);
86 target.format = dateFormater.getDateFormat(patternAlias, locale);
87 if(target.format == null)
88 {
89 throw new Exception("Pattern alias '"+patternAlias+
90 "' not defined in date formatter");
91 }
92 return target;
93 }
94
95 /***
96 * Sets the formatting pattern.
97 *
98 * <p>The syntax of patterns is descirbed in
99 * <code>java.text.SimpleDateFormat</code> documentation.</p>
100 *
101 * @param pattern the formatting pattern.
102 * @return new DateFormatTool instance.
103 */
104 public DateFormatTool pattern(String pattern)
105 {
106 DateFormatTool target = createInstance(this);
107 target.format = new SimpleDateFormat(pattern, locale);
108 return target;
109 }
110
111 /***
112 * Returns the current date and time.
113 *
114 * @return the current date and time.
115 */
116 public Date now()
117 {
118 return new Date();
119 }
120
121 /***
122 * Returns a Date object for a specified UNIX time.
123 *
124 * @param time the UNIX time.
125 * @return a Date object for the specified time.
126 */
127 public Date getDate(long time)
128 {
129 return new Date(time);
130 }
131
132 /***
133 * Formats the Date object.
134 *
135 * @param date the Date.
136 * @return formatted date.
137 */
138 public String format(Date date)
139 {
140 if(date == null)
141 {
142 return null;
143 }
144 return format.format(date);
145 }
146
147 /***
148 * Formats the date given as Unix time.
149 *
150 * @param date the date given as Unix time.
151 * @return formatted date.
152 */
153 public String format(long date)
154 {
155 return format(new Date(date));
156 }
157
158 /***
159 * Returns fields of the specified date using local time zone.
160 *
161 * <p>The field order is as follows:
162 * <ul>
163 * <li>year</li>
164 * <li>month (zero based)</li>
165 * <li>day</li>
166 * <li>hour of day (24h)</li>
167 * <li>minute</li>
168 * <li>second</li>
169 * <li>millisecond</li>
170 * </ul>
171 * </p>
172 * @param date the date to split.
173 * @return list of date fields, as described above.
174 */
175 public List getFields(Date date)
176 {
177 return getFields(date, TimeZone.getDefault());
178 }
179
180 /***
181 * Returns fields of the specified date using specified time zone.
182 *
183 * <p>The field order is as follows:
184 * <ul>
185 * <li>year</li>
186 * <li>month (zero based)</li>
187 * <li>day</li>
188 * <li>hour of day (24h)</li>
189 * <li>minute</li>
190 * <li>second</li>
191 * <li>millisecond</li>
192 * </ul>
193 * </p>
194 * @param date the date to split.
195 * @param zone the timezone to use.
196 * @return list of date fields, as described above.
197 */
198 public List getFields(Date date, TimeZone zone)
199 {
200 ArrayList<Integer> list = new ArrayList<Integer>(7);
201 if(date == null)
202 {
203 return list;
204 }
205 Calendar cal = Calendar.getInstance(zone);
206 cal.setTime(date);
207 list.add(new Integer(cal.get(Calendar.YEAR)));
208 list.add(new Integer(cal.get(Calendar.MONTH)));
209 list.add(new Integer(cal.get(Calendar.DAY_OF_MONTH)));
210 list.add(new Integer(cal.get(Calendar.HOUR_OF_DAY)));
211 list.add(new Integer(cal.get(Calendar.MINUTE)));
212 list.add(new Integer(cal.get(Calendar.SECOND)));
213 list.add(new Integer(cal.get(Calendar.MILLISECOND)));
214 return list;
215 }
216
217 /***
218 * Returns the time zone object with the specified id.
219 *
220 * @param id time zone identifier string.
221 * @return a TimeZone object.
222 */
223 public TimeZone getTimeZone(String id)
224 {
225 return TimeZone.getTimeZone(id);
226 }
227
228 /***
229 * Returns the available time zone ids.
230 *
231 * @return list of recognized time zone ids.
232 */
233 public List getTimeZoneIds()
234 {
235 return Arrays.asList(TimeZone.getAvailableIDs());
236 }
237
238
239
240
241 /***
242 * Creates the DateFormatTool instance for copying. This method is intended to be overriden by
243 * extending classes in order to provide DateFormatTool instances of proper class.
244 *
245 * @param source copied object
246 * @return created instance of the linktool.
247 */
248 protected DateFormatTool createInstance(DateFormatTool source)
249 {
250 return new DateFormatTool(source.dateFormater, source.locale, source.format);
251 }
252 }