KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > pull > util > DateFormatter


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

18
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Date JavaDoc;
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 JavaDoc sdf = new SimpleDateFormat JavaDoc();
37
38     /** Default date format */
39     private static final String JavaDoc 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 JavaDoc DATE_FORMAT_KEY = "tool.dateTool.format";
46
47     private String JavaDoc 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 JavaDoc 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 JavaDoc format(Date JavaDoc 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 JavaDoc format(Date JavaDoc theDate, String JavaDoc dateFormatString)
97     {
98         String JavaDoc 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 }
113
Popular Tags