KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > generic > DateTool


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18
19 package org.apache.velocity.tools.generic;
20
21
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.TimeZone JavaDoc;
28
29
30 /**
31  * Tool for working with {@link Date} and {@link Calendar}
32  * in Velocity templates. It is useful for accessing and
33  * formatting the "current" date as well as for formatting
34  * arbitrary {@link Date} and {@link Calendar} objects. Also
35  * the tool can be used to retrieve {@link DateFormat} instances
36  * or make conversions to and from various date types.
37  * <p><pre>
38  * Example uses:
39  * $date -> Oct 19, 2003 9:54:50 PM
40  * $date.long -> October 19, 2003 9:54:50 PM PDT
41  * $date.medium_time -> 9:54:50 PM
42  * $date.full_date -> Sunday, October 19, 2003
43  * $date.get('default','short') -> Oct 19, 2003 9:54 PM
44  * $date.get('yyyy-M-d H:m:s') -> 2003-10-19 21:54:50
45  *
46  * $myDate -> Tue Oct 07 03:14:50 PDT 2003
47  * $date.format('medium',$myDate) -> Oct 7, 2003 3:14:50 AM
48  *
49  * Example toolbox.xml config (if you want to use this with VelocityView):
50  * &lt;tool&gt;
51  * &lt;key&gt;date&lt;/key&gt;
52  * &lt;scope&gt;application&lt;/scope&gt;
53  * &lt;class&gt;org.apache.velocity.tools.generic.DateTool&lt;/class&gt;
54  * &lt;/tool&gt;
55  * </pre></p>
56  *
57  * <p>This tool is entirely threadsafe, and has no instance members.
58  * It may be used in any scope (request, session, or application).
59  * As such, the methods are highly interconnected, and overriding
60  * key methods provides an easy way to create subclasses that use
61  * a non-default format, calendar, locale, or timezone.</p>
62  *
63  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
64  * @since VelocityTools 1.0
65  * @version $Revision: 1.11.2.1 $ $Date: 2004/03/12 20:16:27 $
66  */

67 public class DateTool
68 {
69
70     /**
71      * The default format to be used when none is specified.
72      *
73      * @since VelocityTools 1.1
74      */

75     public static final String JavaDoc DEFAULT_FORMAT = "default";
76
77     /**
78      * Default constructor.
79      */

80     public DateTool()
81     {
82         // do nothing
83
}
84
85
86     // ------------------------- system date access ------------------
87

88     /**
89      * @return the system's current time as a {@link Date}
90      */

91     public static final Date JavaDoc getSystemDate()
92     {
93         return getSystemCalendar().getTime();
94     }
95
96
97     /**
98      * @return the system's current time as a {@link Calendar}
99      */

100     public static final Calendar JavaDoc getSystemCalendar()
101     {
102         return Calendar.getInstance();
103     }
104
105
106     // ------------------------- default parameter access ----------------
107

108     /**
109      * This implementation returns the default locale. Subclasses
110      * may override this to return alternate locales. Please note that
111      * doing so will affect all formatting methods where no locale is
112      * specified in the parameters.
113      *
114      * @return the default {@link Locale}
115      */

116     public Locale JavaDoc getLocale()
117     {
118         return Locale.getDefault();
119     }
120
121     /**
122      * This implementation returns the default TimeZone. Subclasses
123      * may override this to return alternate timezones. Please note that
124      * doing so will affect all formatting methods where no timezone is
125      * specified in the parameters.
126      *
127      * @return the default {@link TimeZone}
128      */

129     public TimeZone JavaDoc getTimeZone()
130     {
131         return TimeZone.getDefault();
132     }
133
134     /**
135      * Returns a {@link Date} derived from the result of {@link #getCalendar}
136      *
137      * @return a {@link Date} derived from the result of {@link #getCalendar}
138      */

139     public Date JavaDoc getDate()
140     {
141         return getCalendar().getTime();
142     }
143
144     /**
145      * Returns a {@link Calendar} instance created using the timezone and
146      * locale returned by getTimeZone() and getLocale(). This allows subclasses
147      * to easily override the default locale and timezone used by this tool.
148      *
149      * <p>Sub-classes may override this method to return a Calendar instance
150      * not based on the system date.
151      * Doing so will also cause the getDate(), get(String), get(String,String),
152      * and toString() methods to return dates equivalent to the Calendar
153      * returned by this method, because those methods return values derived
154      * from the result of this method.</p>
155      *
156      * @return a {@link Calendar} instance created using the results of
157      * {@link #getTimeZone()} and {@link #getLocale()}.
158      * @see Calendar#getInstance(TimeZone zone, Locale aLocale)
159      */

160     public Calendar JavaDoc getCalendar()
161     {
162         return Calendar.getInstance(getTimeZone(), getLocale());
163     }
164
165     /**
166      * Return the pattern or style to be used for formatting dates when none
167      * is specified. This implementation gives a 'default' date-time format.
168      * Subclasses may override this to provide a different default format.
169      *
170      * <p>NOTE: At some point in the future it may be feasible to configure
171      * this value via the toolbox definition, but at present, it is not possible
172      * to specify custom tool configurations there. For now you should just
173      * override this in a subclass to have a different default.</p>
174      *
175      * @since VelocityTools 1.1
176      */

177     public String JavaDoc getFormat()
178     {
179         return DEFAULT_FORMAT;
180     }
181
182
183     // ------------------------- formatting methods ---------------------------
184

185     /**
186      * @deprecated use {@link #get(String format)} instead. This will be
187      * removed in VelocityTools 1.2
188      */

189     public String JavaDoc getFormattedDate(String JavaDoc format)
190     {
191         return format(format, getDate());
192     }
193
194     /**
195      * Returns a formatted string representing the date returned by
196      * {@link #getDate()}. In its default implementation, this method
197      * allows you to retrieve the current date in standard formats by
198      * simply doing things like <code>$date.medium</code> or
199      * <code>$date.full</code>. If you want only the date or time portion
200      * you can specify that along with the standard formats. (e.g.
201      * <code>$date.medium_date</code> or <code>$date.short_time</code>)
202      * More complex or custom formats can be retrieved
203      * by using the full method syntax. (e.g. $date.get('E, MMMM d'))
204      *
205      * @param format the formatting instructions
206      * @return a formatted representation of the date returned by
207      * {@link #getDate()}
208      * @see #format(String format, Object obj, Locale locale, TimeZone timezone)
209      * @since VelocityTools 1.1
210      */

211     public String JavaDoc get(String JavaDoc format)
212     {
213         return format(format, getDate());
214     }
215
216     /**
217      * Returns a formatted string representing the date and/or time given by
218      * {@link #getDate()} in standard, localized patterns.
219      *
220      * @param dateStyle the style pattern for the date
221      * @param timeStyle the style pattern for the time
222      * @return a formatted representation of the date returned by
223      * {@link #getDate()}
224      * @see DateFormat
225      * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
226      * @since VelocityTools 1.1
227      */

228     public String JavaDoc get(String JavaDoc dateStyle, String JavaDoc timeStyle)
229     {
230         return format(dateStyle, timeStyle, getDate(), getLocale());
231     }
232
233
234     /**
235      * Converts the specified object to a date and formats it according to
236      * the pattern or style returned by {@link #getFormat()}.
237      *
238      * @param obj the date object to be formatted
239      * @return the specified date formatted as a string
240      * @see #format(String format, Object obj, Locale locale, TimeZone timezone)
241      * @since VelocityTools 1.1
242      */

243     public String JavaDoc format(Object JavaDoc obj)
244     {
245         return format(getFormat(), obj);
246     }
247
248     /**
249      * Converts the specified object to a date and returns
250      * a formatted string representing that date in the locale
251      * returned by {@link #getLocale()}.
252      *
253      * @param format the formatting instructions
254      * @param obj the date object to be formatted
255      * @return a formatted string for this locale representing the specified
256      * date or <code>null</code> if the parameters are invalid
257      * @see #format(String format, Object obj, Locale locale, TimeZone timezone)
258      */

259     public String JavaDoc format(String JavaDoc format, Object JavaDoc obj)
260     {
261         return format(format, obj, getLocale());
262     }
263
264     /**
265      * Converts the specified object to a date and returns
266      * a formatted string representing that date in the specified
267      * {@link Locale}.
268      *
269      * @param format the formatting instructions
270      * @param obj the date object to be formatted
271      * @param locale the locale to be used when formatting
272      * @return the given date as a formatted string
273      * @see #format(String format, Object obj, Locale locale, TimeZone timezone)
274      */

275     public String JavaDoc format(String JavaDoc format, Object JavaDoc obj, Locale JavaDoc locale)
276     {
277         return format(format, obj, locale, getTimeZone());
278     }
279
280     /**
281      * Returns a formatted string representing the specified date,
282      * {@link Locale}, and {@link TimeZone}.
283      *
284      * <p>
285      * The specified format may be a standard style pattern ('full', 'long',
286      * 'medium', 'short', or 'default').
287      * </p>
288      * <p>
289      * You may also specify that you want only the date or time portion be
290      * appending '_date' or '_time' respectively to the standard style pattern.
291      * (e.g. 'full_date' or 'long_time')
292      * </p>
293      * <p>
294      * If the format fits neither of these patterns, then the output
295      * will be formatted according to the symbols defined by
296      * {@link SimpleDateFormat}:
297      * <pre>
298      * Symbol Meaning Presentation Example
299      * ------ ------- ------------ -------
300      * G era designator (Text) AD
301      * y year (Number) 1996
302      * M month in year (Text & Number) July & 07
303      * d day in month (Number) 10
304      * h hour in am/pm (1~12) (Number) 12
305      * H hour in day (0~23) (Number) 0
306      * m minute in hour (Number) 30
307      * s second in minute (Number) 55
308      * S millisecond (Number) 978
309      * E day in week (Text) Tuesday
310      * D day in year (Number) 189
311      * F day of week in month (Number) 2 (2nd Wed in July)
312      * w week in year (Number) 27
313      * W week in month (Number) 2
314      * a am/pm marker (Text) PM
315      * k hour in day (1~24) (Number) 24
316      * K hour in am/pm (0~11) (Number) 0
317      * z time zone (Text) Pacific Standard Time
318      * ' escape for text (Delimiter)
319      * '' single quote (Literal) '
320      *
321      * Examples: "E, MMMM d" will result in "Tue, July 24"
322      * "EEE, M-d (H:m)" will result in "Tuesday, 7-24 (14:12)"
323      * </pre>
324      * </p>
325      *
326      * @param format the custom or standard pattern to be used
327      * @param obj the date to format
328      * @param locale the {@link Locale} to format the date for
329      * @param timezone the {@link TimeZone} to be used when formatting
330      * @return a formatted string representing the specified date or
331      * <code>null</code> if the parameters are invalid
332      * @since VelocityTools 1.1
333      */

334     public String JavaDoc format(String JavaDoc format, Object JavaDoc obj,
335                          Locale JavaDoc locale, TimeZone JavaDoc timezone)
336     {
337         Date JavaDoc date = toDate(obj);
338         DateFormat JavaDoc df = getDateFormat(format, locale, timezone);
339         if (date == null || df == null)
340         {
341             return null;
342         }
343         return df.format(date);
344     }
345
346
347     /**
348      * Returns the specified date as a string formatted according to the
349      * specified date and/or time styles.
350      *
351      * @param dateStyle the style pattern for the date
352      * @param timeStyle the style pattern for the time
353      * @param obj the date to be formatted
354      * @return a formatted representation of the given date
355      * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
356      * @since VelocityTools 1.1
357      */

358     public String JavaDoc format(String JavaDoc dateStyle, String JavaDoc timeStyle, Object JavaDoc obj)
359     {
360         return format(dateStyle, timeStyle, obj, getLocale());
361     }
362
363     /**
364      * Returns the specified date as a string formatted according to the
365      * specified {@link Locale} and date and/or time styles.
366      *
367      * @param dateStyle the style pattern for the date
368      * @param timeStyle the style pattern for the time
369      * @param obj the date to be formatted
370      * @param locale the {@link Locale} to be used for formatting the date
371      * @return a formatted representation of the given date
372      * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
373      * @since VelocityTools 1.1
374      */

375     public String JavaDoc format(String JavaDoc dateStyle, String JavaDoc timeStyle,
376                          Object JavaDoc obj, Locale JavaDoc locale)
377     {
378         return format(dateStyle, timeStyle, obj, locale, getTimeZone());
379     }
380
381     /**
382      * Returns the specified date as a string formatted according to the
383      * specified {@link Locale} and date and/or time styles.
384      *
385      * @param dateStyle the style pattern for the date
386      * @param timeStyle the style pattern for the time
387      * @param obj the date to be formatted
388      * @param locale the {@link Locale} to be used for formatting the date
389      * @param timezone the {@link TimeZone} the date should be formatted for
390      * @return a formatted representation of the given date
391      * @see java.text.DateFormat
392      * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
393      * @since VelocityTools 1.1
394      */

395     public String JavaDoc format(String JavaDoc dateStyle, String JavaDoc timeStyle,
396                          Object JavaDoc obj, Locale JavaDoc locale, TimeZone JavaDoc timezone)
397     {
398         Date JavaDoc date = toDate(obj);
399         DateFormat JavaDoc df = getDateFormat(dateStyle, timeStyle, locale, timezone);
400         if (date == null || df == null)
401         {
402             return null;
403         }
404         return df.format(date);
405     }
406
407
408     // -------------------------- DateFormat creation methods --------------
409

410     /**
411      * Returns a {@link DateFormat} instance for the specified
412      * format, {@link Locale}, and {@link TimeZone}. If the format
413      * specified is a standard style pattern, then a date-time instance
414      * will be returned with both the date and time styles set to the
415      * specified style. If it is a custom format, then a customized
416      * {@link SimpleDateFormat} will be returned.
417      *
418      * @param format the custom or standard formatting pattern to be used
419      * @param locale the {@link Locale} to be used
420      * @param timezone the {@link TimeZone} to be used
421      * @return an instance of {@link DateFormat}
422      * @see SimpleDateFormat
423      * @see DateFormat
424      * @since VelocityTools 1.1
425      */

426     public DateFormat JavaDoc getDateFormat(String JavaDoc format, Locale JavaDoc locale,
427                                     TimeZone JavaDoc timezone)
428     {
429         if (format == null)
430         {
431             return null;
432         }
433
434         DateFormat JavaDoc df = null;
435         // do they want a date instance
436
if (format.endsWith("_date"))
437         {
438             String JavaDoc fmt = format.substring(0, format.length() - 5);
439             int style = getStyleAsInt(fmt);
440             df = getDateFormat(style, -1, locale, timezone);
441         }
442         // do they want a time instance?
443
else if (format.endsWith("_time"))
444         {
445             String JavaDoc fmt = format.substring(0, format.length() - 5);
446             int style = getStyleAsInt(fmt);
447             df = getDateFormat(-1, style, locale, timezone);
448         }
449         // ok, they either want a custom or date-time instance
450
else
451         {
452             int style = getStyleAsInt(format);
453             if (style < 0)
454             {
455                 // we have a custom format
456
df = new SimpleDateFormat JavaDoc(format, locale);
457                 df.setTimeZone(timezone);
458             }
459             else
460             {
461                 // they want a date-time instance
462
df = getDateFormat(style, style, locale, timezone);
463             }
464         }
465         return df;
466     }
467
468     /**
469      * Returns a {@link DateFormat} instance for the specified
470      * date style, time style, {@link Locale}, and {@link TimeZone}.
471      *
472      * @param dateStyle the date style
473      * @param timeStyle the time style
474      * @param locale the {@link Locale} to be used
475      * @param timezone the {@link TimeZone} to be used
476      * @return an instance of {@link DateFormat}
477      * @see #getDateFormat(int timeStyle, int dateStyle, Locale locale, TimeZone timezone)
478      * @since VelocityTools 1.1
479      */

480     public DateFormat JavaDoc getDateFormat(String JavaDoc dateStyle, String JavaDoc timeStyle,
481                                     Locale JavaDoc locale, TimeZone JavaDoc timezone)
482     {
483         int ds = getStyleAsInt(dateStyle);
484         int ts = getStyleAsInt(timeStyle);
485         return getDateFormat(ds, ts, locale, timezone);
486     }
487
488     /**
489      * Returns a {@link DateFormat} instance for the specified
490      * time style, date style, {@link Locale}, and {@link TimeZone}.
491      *
492      * @param dateStyle the date style (date will be ignored if this is
493      * less than zero and the date style is not)
494      * @param timeStyle the time style (time will be ignored if this is
495      * less than zero and the date style is not)
496      * @param locale the {@link Locale} to be used
497      * @param timezone the {@link TimeZone} to be used
498      * @return an instance of {@link DateFormat} or <code>null</code>
499      * if an instance cannot be constructed with the given
500      * parameters
501      * @since VelocityTools 1.1
502      */

503     protected DateFormat JavaDoc getDateFormat(int dateStyle, int timeStyle,
504                                        Locale JavaDoc locale, TimeZone JavaDoc timezone)
505     {
506         try
507         {
508             DateFormat JavaDoc df;
509             if (dateStyle < 0 && timeStyle < 0)
510             {
511                 // no style was specified, use default instance
512
df = DateFormat.getInstance();
513             }
514             else if (timeStyle < 0)
515             {
516                 // only a date style was specified
517
df = DateFormat.getDateInstance(dateStyle, locale);
518             }
519             else if (dateStyle < 0)
520             {
521                 // only a time style was specified
522
df = DateFormat.getTimeInstance(timeStyle, locale);
523             }
524             else
525             {
526                 df = DateFormat.getDateTimeInstance(dateStyle, timeStyle,
527                                                     locale);
528             }
529             df.setTimeZone(timezone);
530             return df;
531         }
532         catch (Exception JavaDoc suppressed)
533         {
534             // let it go...
535
return null;
536         }
537     }
538
539     /**
540      * Checks a string to see if it matches one of the standard DateFormat
541      * style patterns: FULL, LONG, MEDIUM, SHORT, or DEFAULT. If it does,
542      * it will return the integer constant for that pattern. If not, it
543      * will return -1.
544      *
545      * @see DateFormat
546      * @param style the string to be checked
547      * @return the int identifying the style pattern
548      * @since VelocityTools 1.1
549      */

550     protected int getStyleAsInt(String JavaDoc style)
551     {
552         // avoid needlessly running through all the string comparisons
553
if (style == null || style.length() < 4 || style.length() > 7) {
554             return -1;
555         }
556         if (style.equalsIgnoreCase("full"))
557         {
558             return DateFormat.FULL;
559         }
560         if (style.equalsIgnoreCase("long"))
561         {
562             return DateFormat.LONG;
563         }
564         if (style.equalsIgnoreCase("medium"))
565         {
566             return DateFormat.MEDIUM;
567         }
568         if (style.equalsIgnoreCase("short"))
569         {
570             return DateFormat.SHORT;
571         }
572         if (style.equalsIgnoreCase("default"))
573         {
574             return DateFormat.DEFAULT;
575         }
576         // ok, it's not any of the standard patterns
577
return -1;
578     }
579
580
581     // ------------------------- date conversion methods ---------------
582

583     /**
584      * Converts an object to an instance of {@link Date} using the
585      * format returned by {@link #getFormat()},the {@link Locale} returned
586      * by {@link #getLocale()}, and the {@link TimeZone} returned by
587      * {@link #getTimeZone()} if the object is not already an instance
588      * of Date, Calendar, or Long.
589      *
590      * @param obj the date to convert
591      * @return the object as a {@link Date} or <code>null</code> if no
592      * conversion is possible
593      */

594     public Date JavaDoc toDate(Object JavaDoc obj)
595     {
596         return toDate(getFormat(), obj, getLocale(), getTimeZone());
597     }
598
599     /**
600      * Converts an object to an instance of {@link Date} using the
601      * specified format,the {@link Locale} returned by
602      * {@link #getLocale()}, and the {@link TimeZone} returned by
603      * {@link #getTimeZone()} if the object is not already an instance
604      * of Date, Calendar, or Long.
605      *
606      * @param format - the format the date is in
607      * @param obj - the date to convert
608      * @return the object as a {@link Date} or <code>null</code> if no
609      * conversion is possible
610      * @see #toDate(String format, Object obj, Locale locale)
611      */

612     public Date JavaDoc toDate(String JavaDoc format, Object JavaDoc obj)
613     {
614         return toDate(format, obj, getLocale(), getTimeZone());
615     }
616
617     /**
618      * Converts an object to an instance of {@link Date} using the
619      * specified format and {@link Locale} if the object is not already
620      * an instance of Date, Calendar, or Long.
621      *
622      * @param format - the format the date is in
623      * @param obj - the date to convert
624      * @param locale - the {@link Locale}
625      * @return the object as a {@link Date} or <code>null</code> if no
626      * conversion is possible
627      * @see SimpleDateFormat#parse
628      */

629     public Date JavaDoc toDate(String JavaDoc format, Object JavaDoc obj, Locale JavaDoc locale)
630     {
631         return toDate(format, obj, locale, getTimeZone());
632     }
633
634     /**
635      * Converts an object to an instance of {@link Date} using the
636      * specified format, {@link Locale}, and {@link TimeZone} if the
637      * object is not already an instance of Date, Calendar, or Long.
638      *
639      * @param format - the format the date is in
640      * @param obj - the date to convert
641      * @param locale - the {@link Locale}
642      * @param timezone - the {@link TimeZone}
643      * @return the object as a {@link Date} or <code>null</code> if no
644      * conversion is possible
645      * @see #getDateFormat
646      * @see SimpleDateFormat#parse
647      */

648     public Date JavaDoc toDate(String JavaDoc format, Object JavaDoc obj,
649                        Locale JavaDoc locale, TimeZone JavaDoc timezone)
650     {
651         if (obj == null)
652         {
653             return null;
654         }
655         if (obj instanceof Date JavaDoc)
656         {
657             return (Date JavaDoc)obj;
658         }
659         if (obj instanceof Calendar JavaDoc)
660         {
661             return ((Calendar JavaDoc)obj).getTime();
662         }
663         if (obj instanceof Long JavaDoc)
664         {
665             Date JavaDoc d = new Date JavaDoc();
666             d.setTime(((Long JavaDoc)obj).longValue());
667             return d;
668         }
669         try
670         {
671             //try parsing w/a customized SimpleDateFormat
672
DateFormat JavaDoc parser = getDateFormat(format, locale, timezone);
673             return parser.parse(String.valueOf(obj));
674         }
675         catch (Exception JavaDoc e)
676         {
677             return null;
678         }
679     }
680
681     /**
682      * Converts an object to an instance of {@link Calendar} using the
683      * locale returned by {@link #getLocale()} if necessary.
684      *
685      * @param obj the date to convert
686      * @return the converted date
687      * @see #toCalendar(Object obj, Locale locale)
688      */

689     public Calendar JavaDoc toCalendar(Object JavaDoc obj)
690     {
691         return toCalendar(obj, getLocale());
692     }
693
694     /**
695      * Converts an object to an instance of {@link Calendar} using the
696      * locale returned by {@link #getLocale()} if necessary.
697      *
698      * @param obj the date to convert
699      * @param locale the locale used
700      * @return the converted date
701      * @see #toDate(String format, Object obj, Locale locale)
702      * @see Calendar
703      */

704     public Calendar JavaDoc toCalendar(Object JavaDoc obj, Locale JavaDoc locale)
705     {
706         if (obj == null)
707         {
708             return null;
709         }
710         if (obj instanceof Calendar JavaDoc)
711         {
712             return (Calendar JavaDoc)obj;
713         }
714         //try to get a date out of it
715
Date JavaDoc date = toDate(obj);
716         if (date == null)
717         {
718             return null;
719         }
720
721         //convert the date to a calendar
722
Calendar JavaDoc cal = Calendar.getInstance(locale);
723         cal.setTime(date);
724         // HACK: Force all fields to update. see link for explanation of this.
725
//http://java.sun.com/j2se/1.4/docs/api/java/util/Calendar.html
726
cal.getTime();
727         return cal;
728     }
729
730
731     // ------------------------- default toString() implementation ------------
732

733     /**
734      * @return the result of {@link #getDate()} formatted according to the result
735      * of {@link #getFormat()}.
736      * @see #format(String format, Object obj)
737      */

738     public String JavaDoc toString()
739     {
740         return format(getFormat(), getDate());
741     }
742
743
744 }
745
Popular Tags