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.text.SimpleDateFormat;
20 import java.util.Date;
21
22 import org.apache.commons.lang.StringUtils;
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 * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a>
30 * @version $Id: DateFormatter.java,v 1.3.2.3 2004/05/20 03:06:47 seade Exp $
31 */
32 public class DateFormatter
33 implements ApplicationTool
34 {
35 /*** Used for formatting date objects */
36 private SimpleDateFormat sdf = new SimpleDateFormat();
37
38 /*** Default date format */
39 private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
40
41 /***
42 * Property tag for the date format that is to be used for the web
43 * application.
44 */
45 private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
46
47 private String dateFormat = null;
48
49 /***
50 * Initialize the application tool. The data parameter holds a different
51 * type depending on how the tool is being instantiated:
52 * <ul>
53 * <li>For global tools data will be null
54 * <li>For request tools data will be of type RunData
55 * <li>For session and persistent tools data will be of type User
56 *
57 * @param data initialization data
58 */
59 public void init(Object data)
60 {
61 dateFormat = Turbine.getConfiguration()
62 .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
63 }
64
65 /***
66 * Refresh the application tool. This is
67 * necessary for development work where you
68 * probably want the tool to refresh itself
69 * if it is using configuration information
70 * that is typically cached after initialization
71 */
72 public void refresh()
73 {
74 }
75
76 /***
77 * Formats the given date as a String using the default date format.
78 * The default date format is MM/dd/yyyy
79 *
80 * @param theDate date to format
81 * @return String value of the date
82 */
83 public String format(Date theDate)
84 {
85 return format(theDate, dateFormat);
86 }
87
88 /***
89 * Formats the given date as a String.
90 *
91 * @param theDate date to format
92 * @param dateFormatString format string to use. See java.text.SimpleDateFormat
93 * for details.
94 * @return String value of the date
95 */
96 public String format(Date theDate, String dateFormatString)
97 {
98 String result = null;
99
100 if (StringUtils.isEmpty(dateFormatString) || theDate == null)
101 {
102 result = "";
103 }
104 else
105 {
106 this.sdf.applyPattern(dateFormatString);
107 result = this.sdf.format(theDate);
108 }
109 return result;
110 }
111
112 }