KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > i18n > FormatDateTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
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
17 package org.apache.taglibs.i18n;
18
19 import java.text.DateFormat JavaDoc;
20 import java.text.Format JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23
24 /** Formats a {@link java.util.Date} instance using a {@link java.util.Locale} and either a
25   * {@link java.text.DateFormat} or a pattern based {@link SimpleDateFormat}.
26   *
27   * @author James Strachan
28   * @version $Revision: 1.5 $
29   */

30 public class FormatDateTag extends FormatDateTagSupport {
31     
32     protected static final String JavaDoc _tagname = "i18n:formatDate";
33
34     private Format JavaDoc format;
35     private String JavaDoc pattern;
36     private int style = DateFormat.SHORT;
37
38
39     // Tag interface
40
//-------------------------------------------------------------------------
41
public void release() {
42         super.release();
43         format = null;
44         pattern = null;
45     }
46     
47     // Properties
48
//-------------------------------------------------------------------------
49

50     /** If no {@link java.text.DateFormat} is configured then use a
51       * {@link SimpleDateFormat} instance if a pattern is configured else
52       * use the default DateFormat for the {@link java.util.Locale}
53       */

54     public Format JavaDoc getFormat() {
55         if ( format == null ) {
56             String JavaDoc pattern = getPattern();
57             if ( pattern != null ) {
58                 format = getPatternFormat( pattern );
59             }
60             if ( pattern == null ) {
61                 format = getDateFormat();
62             }
63         }
64         return format;
65     }
66
67     public void setFormat( DateFormat JavaDoc format ) {
68         this.format = format;
69     }
70
71     public String JavaDoc getPattern() {
72         return pattern;
73     }
74     
75     public void setPattern( String JavaDoc pattern ) {
76         this.pattern = pattern;
77     }
78
79     public void setStyle( String JavaDoc style ) {
80         this.style = getStyleCode( style );
81     }
82     
83     
84     // Implementation methods
85
//-------------------------------------------------------------------------
86
protected DateFormat JavaDoc getPatternFormat( String JavaDoc pattern ) {
87         // XXXX: Use a Locale based cache for these objects as per Craig's
88
// XXXX: suggestion
89
return new SimpleDateFormat JavaDoc( pattern, getLocale() );
90     }
91     
92     protected DateFormat JavaDoc getDateFormat() {
93         return DateFormat.getDateInstance( style, getLocale() );
94     }
95     
96 }
97
Popular Tags