KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > DateTimeFormat


1 /**
2  * $Id: DateTimeFormat.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 1997-2002,2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your option) any later
9  * version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.text.FieldPosition JavaDoc;
32 import java.text.Format JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.TimeZone JavaDoc;
36
37 /**
38  * Helper for formatting time/dates in standard package format. Generated strings are
39  * locale-specific.
40  *
41  * @since JWare/core 0.5
42  * @author ssmc, &copy;1997-2002,2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
43  * @version 0.5
44  * @.safety guarded
45  * @.group impl,helper
46  **/

47
48 public final class DateTimeFormat
49 {
50     private DateTimeFormat() {/*prevent*/}
51
52     /** Abbrev date/time format (not safe for concurrent use). **/
53     public static final SimpleDateFormat JavaDoc ABBREV = new SimpleDateFormat JavaDoc("dd-MMM h:mm a");
54
55     /** Abbrev date format (not safe for concurrent use). **/
56     public static final SimpleDateFormat JavaDoc ABBREV_DATE = new SimpleDateFormat JavaDoc("dd-MMM");
57
58     /** Abbrev time format (not safe for concurrent use).
59      * @since JWare/AntX 0.5
60      **/

61     public static final SimpleDateFormat JavaDoc ABBREV_TIME = new SimpleDateFormat JavaDoc("h:mm a");
62
63     /** Standard date/time format (not safe for concurrent use). **/
64     public static final SimpleDateFormat JavaDoc STANDARD = new SimpleDateFormat JavaDoc("h:mm:ss a dd-MMM-yyyy");
65
66     /** Standard date format (not safe for concurrent use). **/
67     public static final SimpleDateFormat JavaDoc STANDARD_DATE = new SimpleDateFormat JavaDoc("dd-MMM-yyyy");
68
69     /** Standard time format (not safe for concurrent use).
70      * @since JWare/AntX 0.5
71      **/

72     public static final SimpleDateFormat JavaDoc STANDARD_TIME = new SimpleDateFormat JavaDoc("h:mm:ss a");
73
74     /** Standard date format for CVS changelog.
75      * @since JWare/AntX 0.5
76      **/

77     public static final SimpleDateFormat JavaDoc CHANGELOG = new SimpleDateFormat JavaDoc("dd MMM yyyy");
78
79     /** GMT-based date/time format (not safe for concurrent use). **/
80     public static final SimpleDateFormat JavaDoc GMT = new SimpleDateFormat JavaDoc("k:mm:ss dd-MMM-yyyy 'GMT'");
81
82     /** GMT-based date format (not safe for concurrent use). **/
83     public static final SimpleDateFormat JavaDoc GMT_DATE = new SimpleDateFormat JavaDoc("dd-MMM-yyyy 'GMT'");
84
85     /** GMT-based time format (not safe for concurrent use). **/
86     public static final SimpleDateFormat JavaDoc GMT_TIME = new SimpleDateFormat JavaDoc("k:mm:ss 'GMT'");
87
88     static {
89         TimeZone JavaDoc gmtz = TimeZone.getTimeZone("GMT");
90         GMT.setTimeZone(gmtz);
91         GMT_DATE.setTimeZone(gmtz);
92         GMT_TIME.setTimeZone(gmtz);
93     }
94
95
96     /** Converts timestamp into new formatted string; protected against
97         concurrent use.
98         @param tm the timestamp to be formatted
99         @param dfmt the date, time, duration formatter to use (non-null)
100         @throws IllegalArgumentException if formatter is null.
101         @since JWare/AntX 0.5
102      **/

103     public static String JavaDoc format(long tm, Format JavaDoc dfmt) {
104         if (dfmt==null) {
105             throw new IllegalArgumentException JavaDoc();
106         }
107         synchronized(dfmt) {
108             sm_adhoc.setTime(tm);
109             return dfmt.format(sm_adhoc);
110         }
111     }
112
113
114     /** Converts timestamp into new standard formatted string; protected against
115         concurrent use. **/

116     public static String JavaDoc format(long tm) {
117         synchronized(STANDARD) {
118             sm_standard.setTime(tm);
119             return STANDARD.format(sm_standard);
120         }
121     }
122
123     /** Updates existing buffer with timestamp as standard formatted string;
124         protected against concurrent use. **/

125     public static StringBuffer JavaDoc format(long tm, StringBuffer JavaDoc sb) {
126         synchronized(STANDARD) {
127             sm_standard.setTime(tm);
128             return STANDARD.format(sm_standard,sb,new FieldPosition JavaDoc(0));
129         }
130     }
131
132     /** Converts timestamp into new abbreviated formatted string; protected
133         against concurrent use. **/

134     public static String JavaDoc shortformat(long tm) {
135         synchronized(ABBREV) {
136             sm_abbrev.setTime(tm);
137             return ABBREV.format(sm_abbrev);
138         }
139     }
140
141     /** Updates existing buffer with timestamp as abbreviated formatted string;
142         protected against concurrent use. **/

143     public static StringBuffer JavaDoc shortformat(long tm, StringBuffer JavaDoc sb) {
144         synchronized(ABBREV) {
145             sm_abbrev.setTime(tm);
146             return ABBREV.format(sm_abbrev,sb,new FieldPosition JavaDoc(0));
147         }
148     }
149
150     /** Converts timestamp into new GMT formatted string; protected
151         against concurrent use. **/

152     public static String JavaDoc GMTformat(long tm) {
153         synchronized(GMT) {
154             sm_GMT.setTime(tm);
155             return GMT.format(sm_GMT);
156         }
157     }
158
159     /** Updates existing buffer with timestamp as GMT formatted string;
160         protected against concurrent use. **/

161     public static StringBuffer JavaDoc GMTformat(long tm, StringBuffer JavaDoc sb) {
162         synchronized(GMT) {
163             sm_GMT.setTime(tm);
164             return GMT.format(sm_GMT,sb,new FieldPosition JavaDoc(0));
165         }
166     }
167
168     /** Updates existing buffer with date as abbreviated formatted string;
169         protected against concurrent use.
170         @since JWare/AntX 0.4
171      **/

172     public static StringBuffer JavaDoc shortdate(long tm, StringBuffer JavaDoc sb) {
173         synchronized(ABBREV_DATE) {
174             Date JavaDoc dt = new Date JavaDoc(tm);
175             return ABBREV_DATE.format(dt,sb,new FieldPosition JavaDoc(0));
176         }
177     }
178
179     /** Converts timestamp into new short date formatted string; protected
180         against concurrent use.
181         @since JWare/AntX 0.4
182      **/

183     public static String JavaDoc shortdate(long tm) {
184         synchronized(ABBREV_DATE) {
185             Date JavaDoc dt = new Date JavaDoc(tm);
186             return ABBREV_DATE.format(dt);
187         }
188     }
189
190
191
192     private static final Date JavaDoc sm_abbrev = new Date JavaDoc();
193     private static final Date JavaDoc sm_standard = new Date JavaDoc();
194     private static final Date JavaDoc sm_GMT = new Date JavaDoc();
195     private static final Date JavaDoc sm_adhoc = new Date JavaDoc();
196 }
197
198 /* end-of-DateTimeFormat.java */
199
Popular Tags