001 package org.apache.turbine.services.pull.util;
002
003
004 /*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements. See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership. The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License. You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied. See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024 import java.text.SimpleDateFormat;
025 import java.util.Date;
026
027 import org.apache.commons.lang.StringUtils;
028 import org.apache.turbine.Turbine;
029 import org.apache.turbine.services.pull.ApplicationTool;
030
031 /**
032 * This pull tool is used to format date objects into strings.
033 *
034 * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a>
035 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
036 * @version $Id: DateFormatter.java 1078552 2011-03-06 19:58:46Z tv $
037 */
038 public class DateFormatter
039 implements ApplicationTool
040 {
041 /** Used for formatting date objects */
042 private final SimpleDateFormat sdf = new SimpleDateFormat();
043
044 /** Default date format */
045 private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
046
047 /**
048 * Property tag for the date format that is to be used for the web
049 * application.
050 */
051 private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
052
053 private String dateFormat = null;
054
055 /**
056 * Initialize the application tool. The data parameter holds a different
057 * type depending on how the tool is being instantiated:
058 * <ul>
059 * <li>For global tools data will be null
060 * <li>For request tools data will be of type RunData
061 * <li>For session and persistent tools data will be of type User
062 *
063 * @param data initialization data
064 */
065 public void init(Object data)
066 {
067 dateFormat = Turbine.getConfiguration()
068 .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
069 }
070
071 /**
072 * Refresh the application tool. This is
073 * necessary for development work where you
074 * probably want the tool to refresh itself
075 * if it is using configuration information
076 * that is typically cached after initialization
077 */
078 public void refresh()
079 {
080 // empty
081 }
082
083 /**
084 * Formats the given date as a String using the default date format.
085 * The default date format is MM/dd/yyyy
086 *
087 * @param theDate date to format
088 * @return String value of the date
089 */
090 public String format(Date theDate)
091 {
092 return format(theDate, dateFormat);
093 }
094
095 /**
096 * Formats the given date as a String.
097 *
098 * @param theDate date to format
099 * @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 }