1 package org.apache.turbine.services.pull.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Date;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.commons.lang.time.DateFormatUtils;
23 import org.apache.turbine.Turbine;
24 import org.apache.turbine.services.pull.ApplicationTool;
25
26 /***
27 * This pull tool is used to format date objects into strings.
28 *
29 * <p>As this is designed to be used as a gloal scope pull tool it needs to be
30 * threadsafe.
31 *
32 * <p>This is an application pull tool for the template system. You should
33 * <b>not</b> use it in a normal application.
34 *
35 * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a>
36 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
37 * @version $Id: DateFormatter.java 264148 2005-08-29 14:21:04Z henning $
38 */
39 public class DateFormatter
40 implements ApplicationTool
41 {
42 /*** Default date format */
43 private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
44
45 /***
46 * Property tag for the date format that is to be used for the web
47 * application.
48 */
49 private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
50
51 private String dateFormat = null;
52
53 /***
54 * Initialize the application tool. The data parameter holds a different
55 * type depending on how the tool is being instantiated:
56 * <ul>
57 * <li>For global tools data will be null
58 * <li>For request tools data will be of type RunData
59 * <li>For session and persistent tools data will be of type User
60 *
61 * @param data initialization data
62 */
63 public void init(Object data)
64 {
65 dateFormat = Turbine.getConfiguration()
66 .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
67 }
68
69 /***
70 * Refresh the application tool. This is necessary for development work
71 * where you probably want the tool to refresh itself if it is using
72 * configuration information that is typically cached after initialization.
73 */
74 public void refresh()
75 {
76 }
77
78 /***
79 * Formats the given date as a String using the default date format.
80 * The default date format is MM/dd/yyyy
81 *
82 * @param theDate date to format
83 * @return String value of the date
84 */
85 public String format(Date theDate)
86 {
87 return format(theDate, dateFormat);
88 }
89
90 /***
91 * Formats the given date as a String.
92 *
93 * @param theDate date to format
94 * @param dateFormatString format string to use. See
95 * java.text.SimpleDateFormat for details.
96 * @return String value of the date
97 */
98 public String format(Date theDate, String dateFormatString)
99 {
100 String result = null;
101
102 if (StringUtils.isEmpty(dateFormatString) || theDate == null)
103 {
104 result = "";
105 }
106 else
107 {
108 result = DateFormatUtils.format(theDate, dateFormatString);
109 }
110 return result;
111 }
112
113 }