View Javadoc

1   package org.apache.turbine.services.pull.util;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.turbine.Turbine;
29  import org.apache.turbine.services.pull.ApplicationTool;
30  
31  /**
32   * This pull tool is used to format date objects into strings.
33   *
34   * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a>
35   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
36   * @version $Id: DateFormatter.java 1078552 2011-03-06 19:58:46Z tv $
37   */
38  public class DateFormatter
39          implements ApplicationTool
40  {
41      /** Used for formatting date objects */
42      private final SimpleDateFormat sdf = new SimpleDateFormat();
43  
44      /** Default date format */
45      private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
46  
47      /**
48       * Property tag for the date format that is to be used for the web
49       * application.
50       */
51      private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
52  
53      private String dateFormat = null;
54  
55      /**
56       * Initialize the application tool. The data parameter holds a different
57       * type depending on how the tool is being instantiated:
58       * <ul>
59       * <li>For global tools data will be null
60       * <li>For request tools data will be of type RunData
61       * <li>For session and persistent tools data will be of type User
62       *
63       * @param data initialization data
64       */
65      public void init(Object data)
66      {
67          dateFormat = Turbine.getConfiguration()
68                  .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
69      }
70  
71      /**
72       * Refresh the application tool. This is
73       * necessary for development work where you
74       * probably want the tool to refresh itself
75       * if it is using configuration information
76       * that is typically cached after initialization
77       */
78      public void refresh()
79      {
80          // empty
81      }
82  
83      /**
84       * Formats the given date as a String using the default date format.
85       * The default date format is MM/dd/yyyy
86       *
87       * @param theDate date to format
88       * @return String value of the date
89       */
90      public String format(Date theDate)
91      {
92          return format(theDate, dateFormat);
93      }
94  
95      /**
96       * Formats the given date as a String.
97       *
98       * @param theDate date to format
99       * @param dateFormatString format string to use.  See java.text.SimpleDateFormat
100      * for details.
101      * @return String value of the date
102      */
103     public String format(Date theDate, String dateFormatString)
104     {
105         String result = null;
106 
107         if (StringUtils.isEmpty(dateFormatString) || theDate == null)
108         {
109             result = "";
110         }
111         else
112         {
113             this.sdf.applyPattern(dateFormatString);
114             result = this.sdf.format(theDate);
115         }
116         return result;
117     }
118 
119 }