1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8 package org.apache.james.util;
9
10 import java.text.ParseException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.Locale;
14 import java.util.TimeZone;
15
16 /***
17 * A utility class to allow creation of RFC822 date strings from Dates
18 * and dates from RFC822 strings<br>
19 * It provides for conversion between timezones,
20 * And easy manipulation of RFC822 dates<br>
21 * example - current timestamp: String nowdate = new RFC822Date().toString()<br>
22 * example - convert into java.util.Date: Date usedate = new RFC822Date("3 Oct 2001 08:32:44 -0000").getDate()<br>
23 * example - convert to timezone: String yourdate = new RFC822Date("3 Oct 2001 08:32:44 -0000", "GMT+02:00").toString()<br>
24 * example - convert to local timezone: String mydate = new RFC822Date("3 Oct 2001 08:32:44 -0000").toString()<br>
25 * @author Danny Angus (danny) <Danny@thought.co.uk><br>
26 * @author Peter M. Goldstein <farsight@alum.mit.edu><br>
27 *
28 * @deprecated Use java.util.Date in combination with org.apache.james.util.RFC822DateFormat.
29 */
30 public class RFC822Date {
31 private static SimpleDateFormat df;
32 private static SimpleDateFormat dx;
33 private static SimpleDateFormat dy;
34 private static SimpleDateFormat dz;
35 private Date d;
36 private RFC822DateFormat rfc822Format = new RFC822DateFormat();
37
38 static {
39 df = new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss", Locale.US);
40 dx = new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss zzzzz", Locale.US);
41 dy = new SimpleDateFormat("EE d MMM yyyy HH:mm:ss zzzzz", Locale.US);
42 dz = new SimpleDateFormat("d MMM yyyy HH:mm:ss zzzzz", Locale.US);
43 }
44
45 /***
46 * creates a current timestamp
47 * using this machines system timezone<br>
48 *
49 */
50 public RFC822Date(){
51 d = new Date();
52 }
53
54 /***
55 * creates object using date supplied
56 * and this machines system timezone<br>
57 * @param da java.util.Date, A date object
58 */
59 public RFC822Date(Date da) {
60 d = da;
61 }
62
63 /***
64 * creates object using date supplied
65 * and the timezone string supplied<br>
66 * useTZ can be either an abbreviation such as "PST",
67 * a full name such as "America/Los_Angeles",<br>
68 * or a custom ID such as "GMT-8:00".<br>
69 * Note that this is dependant on java.util.TimeZone<br>
70 * Note that the support of abbreviations is for
71 * JDK 1.1.x compatibility only and full names should be used.<br>
72 * @param da java.util.Date, a date object
73 * @param useTZ java.lang.Sting, a timezone string such as "America/Los_Angeles" or "GMT+02:00"
74 */
75 public RFC822Date(Date da, String useTZ){
76 d = da;
77 }
78
79 /***
80 * creates object from
81 * RFC822 date string supplied
82 * and the system default time zone <br>
83 * In practice it converts RFC822 date string to the local timezone<br>
84 * @param rfcdate java.lang.String - date in RFC822 format "3 Oct 2001 08:32:44 -0000"
85 */
86 public RFC822Date(String rfcdate) {
87 setDate(rfcdate);
88 }
89 /***
90 * creates object from
91 * RFC822 date string supplied
92 * using the supplied time zone string<br>
93 * @param rfcdate java.lang.String - date in RFC822 format
94 * @param useTZ java.lang.String - timezone string *doesn't support Z style or UT*
95 */
96 public RFC822Date(String rfcdate, String useTZ) {
97 setDate(rfcdate);
98 setTimeZone(useTZ);
99 }
100
101 public void setDate(Date da){
102 d = da;
103 }
104
105 /***
106 * The following styles of rfc date strings can be parsed<br>
107 * Wed, 3 Oct 2001 06:42:27 GMT+02:10<br>
108 * Wed 3 Oct 2001 06:42:27 PST <br>
109 * 3 October 2001 06:42:27 +0100 <br>
110 * the military style timezones, ZM, ZA, etc cannot (yet) <br>
111 * @param rfcdate java.lang.String - date in RFC822 format
112 */
113 public void setDate(String rfcdate) {
114 try {
115 synchronized (dx) {
116 d= dx.parse(rfcdate);
117 }
118 } catch(ParseException e) {
119 try {
120 synchronized (dz) {
121 d= dz.parse(rfcdate);
122 }
123 } catch(ParseException f) {
124 try {
125 synchronized (dy) {
126 d = dy.parse(rfcdate);
127 }
128 } catch(ParseException g) {
129 d = new Date();
130 }
131 }
132
133 }
134
135 }
136
137 public void setTimeZone(TimeZone useTZ) {
138 rfc822Format.setTimeZone(useTZ);
139 }
140
141 public void setTimeZone(String useTZ) {
142 setTimeZone(TimeZone.getTimeZone(useTZ));
143 }
144
145
146 /***
147 * returns the java.util.Date object this RFC822Date represents.
148 * @return java.util.Date - the java.util.Date object this RFC822Date represents.
149 */
150 public Date getDate() {
151 return d;
152 }
153
154 /***
155 * returns the date as a string formated for RFC822 compliance
156 * ,accounting for timezone and daylight saving.
157 * @return java.lang.String - date as a string formated for RFC822 compliance
158 *
159 */
160 public String toString() {
161 return rfc822Format.format(d);
162 }
163 }
This page was automatically generated by Maven