    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 1773378 2016-12-09 13:19:59Z tv $
    037 */
    038public class DateFormatter
    039        implements ApplicationTool
    040{
    041    /** Default date format */
    042    private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
    043
    044    /**
    045     * Property tag for the date format that is to be used for the web
    046     * application.
    047     */
    048    private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
    049
    050    private String dateFormat = null;
    051
    052    /**
    053     * Initialize the application tool. The data parameter holds a different
    054     * type depending on how the tool is being instantiated:
    055     * <ul>
    056     * <li>For global tools data will be null</li>
    057     * <li>For request tools data will be of type RunData</li>
    058     * <li>For session and persistent tools data will be of type User</li>
    059     * </ul>
    060     *
    061     * @param data initialization data
    062     */
    063    @Override
    064    public void init(Object data)
    065    {
    066        dateFormat = Turbine.getConfiguration()
    067                .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
    068    }
    069
    070    /**
    071     * Refresh the application tool. This is
    072     * necessary for development work where you
    073     * probably want the tool to refresh itself
    074     * if it is using configuration information
    075     * that is typically cached after initialization
    076     */
    077    @Override
    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        SimpleDateFormat sdf = new SimpleDateFormat();
    107
    108        if (StringUtils.isEmpty(dateFormatString) || theDate == null)
    109        {
    110            result = "";
    111        }
    112        else
    113        {
    114            sdf.applyPattern(dateFormatString);
    115            result = sdf.format(theDate);
    116        }
    117        return result;
    118    }
    119}