Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/web/FormattedDate.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: FormattedDate.java,v $
5    * $Date: 2003/02/26 03:38:46 $
6    * $Revision: 1.3 $
7    */
8   
9   package com.clra.web;
10  
11  import java.io.Serializable;
12  import java.text.DateFormat;
13  import java.text.SimpleDateFormat;
14  import java.util.Calendar;
15  import java.util.Date;
16  import java.util.GregorianCalendar;
17  import javax.servlet.jsp.JspException;
18  
19  /**
20   * A JSP bean that displays a formatted date. This class is a thin wrapper
21   * around the Date and SimpleDateFormat classe. See the SimpleDateFormat
22   * for how date formats are specified.<p>
23   *
24   * @version $Id: FormattedDate.java,v 1.3 2003/02/26 03:38:46 rphall Exp $
25   * @author <a href="mailto:rphall@pluto.njcc.com>Rick Hall</a>
26   * @see java.text.SimpleDateFormat
27   */
28  
29  public class FormattedDate implements Serializable {
30  
31    /** Default format is "Tuesday, 10/28/99" */
32    public final static String DEFAULT_FORMAT = "EEEE', 'MM/dd/yy";
33  
34    /** Default date is the current system date/time */
35    private Date date = new Date();
36  
37    /** Default format specification is specified by DEFAULT_FORMAT */
38    private String format = DEFAULT_FORMAT;
39  
40    /**
41     * This member should cache the currently applied format/date.
42     * See the constructors and setters.
43     */
44    private String value = null;
45  
46    /** Utility that applies a format to a date */
47    public static String applyFormat( String fmt, Date dt )
48        throws JspException {
49  
50      // Preconditions
51      if ( fmt == null || fmt.trim().length() == 0 ) {
52        throw new JspException( "invalid format == '" + fmt + "'" );
53      }
54      if ( dt == null ) {
55        throw new JspException( "null dt" );
56      }
57  
58      String retVal = null;
59      try {
60        SimpleDateFormat sdf = new SimpleDateFormat( fmt );
61        retVal = sdf.format( dt );
62      }
63      catch( Exception x ) {
64        throw new JspException( x.toString() );
65      }
66  
67      // Enforce postcondition
68      if ( retVal == null ) { throw new Error( "design error" ); }
69  
70      return retVal;
71    } // applyFormat(String,Date)
72  
73    // CONSTRUCTORS
74  
75    /**
76     * Constructs a formatted date the current system time with
77     * <tt>DEFAULT_FORMAT</tt>
78     */
79    public FormattedDate() throws JspException {
80      this.value = applyFormat( this.format, this.date );
81    }
82  
83    /**
84     * Constructs a formatted date with the current system time
85     * and the specified format
86     */
87    public FormattedDate( String format ) throws JspException {
88      // Preconditions checked by applyFormat
89      setFormat( format );
90    }
91  
92    /**
93     * Constructs a formatted date with the specified datetime
94     * and <tt>DEFAULT_FORMAT</tt>
95     */
96    public FormattedDate( Date date ) throws JspException {
97      // Preconditions checked by applyFormat
98      setDate( date );
99    }
100 
101   /**
102    * Constructs a formatted date with the specified datetime
103    * and format.
104    */
105   public FormattedDate( String format, Date date ) throws JspException {
106     // Preconditions checked by applyFormat
107     this.format = format;
108     this.date = date;
109     this.value = applyFormat( format, date );
110   } // FormattedDate(String,Date)
111 
112   /**
113    * Constructs a formatted date with the specified month (0-11), year
114    * (four digits) and format.
115    */
116   public FormattedDate( String format, Integer month, Integer year )
117     throws JspException {
118 
119     // FIXME Preconditions
120     Calendar calendar = new GregorianCalendar();
121     calendar.set( Calendar.YEAR, year.intValue() );
122     calendar.set( Calendar.MONTH, month.intValue() );
123 
124     // Set the date in the middle of the month
125     calendar.set( Calendar.DATE, 15 );
126 
127     this.format = format;
128     this.date = calendar.getTime();
129     this.value = applyFormat( format, date );
130 
131   } // FormattedDate(String,Date)
132 
133   // PROPERTY ACCESSORS AND MANIPULATORS
134 
135   public Date getDate() {
136     return this.date;
137   }
138 
139   public void setDate( Date date ) throws JspException {
140     // Preconditions checked by applyFormat
141     this.date = date;
142     this.value = applyFormat( this.format, this.date );
143   }
144 
145   public String getFormat()  {
146     return this.format;
147   }
148 
149   public void setFormat( String format ) throws JspException {
150     // Preconditions checked by applyFormat
151     this.format = format;
152     this.value = applyFormat( this.format, this.date );
153   }
154 
155   public String getValue() {
156     return this.value;
157   }
158 
159 } // FormattedDate
160 
161 /*
162  * $Log: FormattedDate.java,v $
163  * Revision 1.3  2003/02/26 03:38:46  rphall
164  * Added copyright and GPL license
165  *
166  * Revision 1.2  2002/02/18 18:05:58  rphall
167  * Ran dos2unix to remove ^M (carriage return) from end of lines
168  *
169  * Revision 1.1.1.1  2002/01/03 21:57:28  rphall
170  * Initial load, 5th try, Jan-03-2002 4:57 PM
171  *
172  * Revision 1.3  2001/12/14 02:20:36  rphall
173  * Added constructor taking Integer month, year
174  *
175  * Revision 1.2  2001/11/28 12:09:48  rphall
176  * Made Serializable
177  *
178  * Revision 1.1  2001/11/23 19:40:02  rphall
179  * Major revision
180  *
181  */
182