001package 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 024import java.text.SimpleDateFormat; 025import java.util.Date; 026 027import org.apache.commons.lang.StringUtils; 028import org.apache.turbine.Turbine; 029import 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 1706239 2015-10-01 13:18:35Z tv $ 037 */ 038public 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 @Override 066 public void init(Object data) 067 { 068 dateFormat = Turbine.getConfiguration() 069 .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT); 070 } 071 072 /** 073 * Refresh the application tool. This is 074 * necessary for development work where you 075 * probably want the tool to refresh itself 076 * if it is using configuration information 077 * that is typically cached after initialization 078 */ 079 @Override 080 public void refresh() 081 { 082 // empty 083 } 084 085 /** 086 * Formats the given date as a String using the default date format. 087 * The default date format is MM/dd/yyyy 088 * 089 * @param theDate date to format 090 * @return String value of the date 091 */ 092 public String format(Date theDate) 093 { 094 return format(theDate, dateFormat); 095 } 096 097 /** 098 * Formats the given date as a String. 099 * 100 * @param theDate date to format 101 * @param dateFormatString format string to use. See java.text.SimpleDateFormat 102 * for details. 103 * @return String value of the date 104 */ 105 public String format(Date theDate, String dateFormatString) 106 { 107 String result = null; 108 109 if (StringUtils.isEmpty(dateFormatString) || theDate == null) 110 { 111 result = ""; 112 } 113 else 114 { 115 this.sdf.applyPattern(dateFormatString); 116 result = this.sdf.format(theDate); 117 } 118 return result; 119 } 120 121}