View Javadoc

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