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 1706239 2015-10-01 13:18:35Z 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      @Override
    66      public void init(Object data)
    67      {
    68          dateFormat = Turbine.getConfiguration()
    69                  .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
    70      }
    71
    72      /**
    73       * Refresh the application tool. This is
    74       * necessary for development work where you
    75       * probably want the tool to refresh itself
    76       * if it is using configuration information
    77       * that is typically cached after initialization
    78       */
    79      @Override
    80      public void refresh()
    81      {
    82          // empty
    83      }
    84
    85      /**
    86       * Formats the given date as a String using the default date format.
    87       * The default date format is MM/dd/yyyy
    88       *
    89       * @param theDate date to format
    90       * @return String value of the date
    91       */
    92      public String format(Date theDate)
    93      {
    94          return format(theDate, dateFormat);
    95      }
    96
    97      /**
    98       * Formats the given date as a String.
    99       *
    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 }