KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > RFC822Date


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util;
19
20 import java.text.ParseException JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.TimeZone JavaDoc;
25
26 /**
27  * A utility class to allow creation of RFC822 date strings from Dates
28  * and dates from RFC822 strings<br>
29  * It provides for conversion between timezones,
30  * And easy manipulation of RFC822 dates<br>
31  * example - current timestamp: String nowdate = new RFC822Date().toString()<br>
32  * example - convert into java.util.Date: Date usedate = new RFC822Date("3 Oct 2001 08:32:44 -0000").getDate()<br>
33  * example - convert to timezone: String yourdate = new RFC822Date("3 Oct 2001 08:32:44 -0000", "GMT+02:00").toString()<br>
34  * example - convert to local timezone: String mydate = new RFC822Date("3 Oct 2001 08:32:44 -0000").toString()<br>
35  *
36  * @deprecated Use java.util.Date in combination with org.apache.james.util.RFC822DateFormat.
37  */

38 public class RFC822Date {
39     private static SimpleDateFormat JavaDoc df;
40     private static SimpleDateFormat JavaDoc dx;
41     private static SimpleDateFormat JavaDoc dy;
42     private static SimpleDateFormat JavaDoc dz;
43     private Date JavaDoc d;
44     private RFC822DateFormat rfc822Format = new RFC822DateFormat();
45    
46     static {
47         df = new SimpleDateFormat JavaDoc("EE, d MMM yyyy HH:mm:ss", Locale.US);
48         dx = new SimpleDateFormat JavaDoc("EE, d MMM yyyy HH:mm:ss zzzzz", Locale.US);
49         dy = new SimpleDateFormat JavaDoc("EE d MMM yyyy HH:mm:ss zzzzz", Locale.US);
50         dz = new SimpleDateFormat JavaDoc("d MMM yyyy HH:mm:ss zzzzz", Locale.US);
51       }
52    
53    /**
54     * creates a current timestamp
55     * using this machines system timezone<br>
56     *
57     */

58     public RFC822Date(){
59         d = new Date JavaDoc();
60     }
61     
62    /**
63     * creates object using date supplied
64     * and this machines system timezone<br>
65     * @param da java.util.Date, A date object
66     */

67     public RFC822Date(Date JavaDoc da) {
68         d = da;
69     }
70     
71    /**
72     * creates object using date supplied
73     * and the timezone string supplied<br>
74     * useTZ can be either an abbreviation such as "PST",
75     * a full name such as "America/Los_Angeles",<br>
76     * or a custom ID such as "GMT-8:00".<br>
77     * Note that this is dependant on java.util.TimeZone<br>
78     * Note that the support of abbreviations is for
79     * JDK 1.1.x compatibility only and full names should be used.<br>
80     * @param da java.util.Date, a date object
81     * @param useTZ java.lang.Sting, a timezone string such as "America/Los_Angeles" or "GMT+02:00"
82     */

83     public RFC822Date(Date JavaDoc da, String JavaDoc useTZ){
84         d = da;
85     }
86
87     /**
88     * creates object from
89     * RFC822 date string supplied
90     * and the system default time zone <br>
91     * In practice it converts RFC822 date string to the local timezone<br>
92     * @param rfcdate java.lang.String - date in RFC822 format "3 Oct 2001 08:32:44 -0000"
93     */

94     public RFC822Date(String JavaDoc rfcdate) {
95         setDate(rfcdate);
96     }
97     /**
98     * creates object from
99     * RFC822 date string supplied
100     * using the supplied time zone string<br>
101     * @param rfcdate java.lang.String - date in RFC822 format
102     * @param useTZ java.lang.String - timezone string *doesn't support Z style or UT*
103     */

104     public RFC822Date(String JavaDoc rfcdate, String JavaDoc useTZ) {
105         setDate(rfcdate);
106         setTimeZone(useTZ);
107     }
108
109     public void setDate(Date JavaDoc da){
110         d = da;
111     }
112     
113  /**
114  * The following styles of rfc date strings can be parsed<br>
115  * Wed, 3 Oct 2001 06:42:27 GMT+02:10<br>
116  * Wed 3 Oct 2001 06:42:27 PST <br>
117  * 3 October 2001 06:42:27 +0100 <br>
118  * the military style timezones, ZM, ZA, etc cannot (yet) <br>
119  * @param rfcdate java.lang.String - date in RFC822 format
120  */

121     public void setDate(String JavaDoc rfcdate) {
122         try {
123             synchronized (dx) {
124                 d= dx.parse(rfcdate);
125             }
126         } catch(ParseException JavaDoc e) {
127             try {
128                 synchronized (dz) {
129                     d= dz.parse(rfcdate);
130                 }
131             } catch(ParseException JavaDoc f) {
132                 try {
133                     synchronized (dy) {
134                         d = dy.parse(rfcdate);
135                     }
136                 } catch(ParseException JavaDoc g) {
137                     d = new Date JavaDoc();
138                 }
139             }
140             
141         }
142         
143     }
144  
145     public void setTimeZone(TimeZone JavaDoc useTZ) {
146         rfc822Format.setTimeZone(useTZ);
147     }
148     
149     public void setTimeZone(String JavaDoc useTZ) {
150         setTimeZone(TimeZone.getTimeZone(useTZ));
151     }
152     
153
154     /**
155      * returns the java.util.Date object this RFC822Date represents.
156      * @return java.util.Date - the java.util.Date object this RFC822Date represents.
157      */

158     public Date JavaDoc getDate() {
159         return d;
160     }
161
162     /**
163      * returns the date as a string formated for RFC822 compliance
164      * ,accounting for timezone and daylight saving.
165      * @return java.lang.String - date as a string formated for RFC822 compliance
166      *
167      */

168     public String JavaDoc toString() {
169         return rfc822Format.format(d);
170     }
171 }
172
Popular Tags