View Javadoc

    1   /*
    2    * $Id: DateFormatter.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.util;
    22
    23  import java.text.DateFormat;
    24  import java.text.ParseException;
    25  import java.text.SimpleDateFormat;
    26  import java.util.Date;
    27
    28
    29  /***
    30   * A bean that can be used to format dates
    31   *
    32   */
    33  public class DateFormatter {
    34
    35      Date date;
    36      DateFormat format;
    37
    38      // Attributes ----------------------------------------------------
    39      DateFormat parser;
    40
    41
    42      // Public --------------------------------------------------------
    43      public DateFormatter() {
    44          this.parser = new SimpleDateFormat();
    45          this.format = new SimpleDateFormat();
    46          this.date = new Date();
    47      }
    48
    49
    50      public void setDate(String date) {
    51          try {
    52              this.date = parser.parse(date);
    53          } catch (ParseException e) {
    54              throw new IllegalArgumentException(e.getMessage());
    55          }
    56      }
    57
    58      public void setDate(Date date) {
    59          this.date = date;
    60      }
    61
    62      public void setDate(int date) {
    63          setDate(Integer.toString(date));
    64      }
    65
    66      public Date getDate() {
    67          return this.date;
    68      }
    69
    70      public void setFormat(String format) {
    71          this.format = new SimpleDateFormat(format);
    72      }
    73
    74      public void setFormat(DateFormat format) {
    75          this.format = format;
    76      }
    77
    78      public String getFormattedDate() {
    79          return format.format(date);
    80      }
    81
    82      public void setParseFormat(String format) {
    83          this.parser = new SimpleDateFormat(format);
    84      }
    85
    86      public void setParser(DateFormat parser) {
    87          this.parser = parser;
    88      }
    89
    90      public void setTime(long time) {
    91          date.setTime(time);
    92      }
    93  }