View Javadoc
1   /*
2    * Copyright 2005 Filipe Tavares
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.devyant.decorutils.decorators;
17  
18  import org.devyant.decorutils.Decorator;
19  
20  import javax.servlet.jsp.PageContext;
21  import java.text.DateFormat;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.Locale;
25  
26  /***
27   * Decorator for transforming <code>Date</code> instances
28   * for example and utility purpose.
29   *
30   * @author Filipe Tavares
31   * @version $Revision: 1.0$ ($Author: ftavares$)
32   * @since 16/Fev/2005 4:34:28
33   */
34  public class DateDecorator implements Decorator {
35      /***
36       * The DEFAULT_LOCALE <code>Locale</code>.
37       */
38      public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
39      /***
40       * The <code>Locale</code> to be used.
41       */
42      private transient Locale locale = DEFAULT_LOCALE;
43      /***
44       * <code>DateFormat</code> constant value.
45       */
46      private transient int dateFormat = DateFormat.FULL;
47  
48      /***
49       * Custom format for the date.
50       */
51      private transient String customFormat = null;
52  
53      /***
54       * The decorate method.
55       * @param object Object to decorate
56       * @return The decorated object
57       */
58      public final Object decorate(final Object object) {
59          return getDateString((Date) object);
60      }
61      /***
62       * <p>Format <code>Date</code> and return resulting <code>String</code>.</p>
63       * @param date The date instance
64       * @return Formatted <code>String</code>
65       */
66      private String getDateString(final Date date) {
67          return getDateFormatter().format(date);
68      }
69  
70      /***
71       * <p>Return the corresponding <code>DateFormat</code> instance.</p>
72       * @return The <code>DateFormat</code> instance
73       */
74      private DateFormat getDateFormatter() {
75          if (customFormat == null) {
76              return DateFormat.getDateInstance(dateFormat, locale);
77          } else {
78              return new SimpleDateFormat(customFormat, locale);
79          }
80      }
81  
82      /***
83       * <p>Setter method for the <i>pageContext</i> attribute.</p>
84       * @param pageContext The page context
85       */
86      public final void setPageContext(final PageContext pageContext) {
87          this.locale = pageContext.getRequest().getLocale();
88      }
89  
90      /***
91       * <p>Setter method for the <i>format</i> attribute.</p>
92       * @param format The date format
93       */
94      public final void setFormat(final String format) {
95          if (format.equalsIgnoreCase("full")) {
96              dateFormat = DateFormat.FULL;
97          } else if (format.equalsIgnoreCase("long")) {
98              dateFormat = DateFormat.LONG;
99          } else if (format.equalsIgnoreCase("medium")) {
100             dateFormat = DateFormat.MEDIUM;
101         } else if (format.equalsIgnoreCase("short")) {
102             dateFormat = DateFormat.SHORT;
103         } else if (format.equalsIgnoreCase("fixed")) {
104             customFormat = "dd MMMM yyyy HH:mm";
105         } else {
106             customFormat = format;
107         }
108     }
109 }