KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > RfcDateFormat


1 /*
2  * Copyright 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 tutorial;
18
19 import java.util.Date JavaDoc;
20
21 /**
22  * Alternative RFC 1123 data formatter. SimpleDataFormat class has thread-safe
23  * issues as documented by Sun. This formatter is based on code provided by
24  * Tim Kientzle on the Tomcat list-serv and made available for free public use.
25  * @see http://marc.theaimsgroup.com/?l=tomcat-dev&m=97146648030873&w=2
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  */

28 public class RfcDateFormat {
29
30     /**
31      * Enumeration of the days of the week.
32      */

33     private static final String JavaDoc[] WEEKDAYS =
34         {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
35     /**
36      * Enumeration of the months of the year.
37      */

38     private static final String JavaDoc[] MONTHS =
39         {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
40
41     /**
42      * Formats a date object according to RFC 1123.
43      * <p>
44      * An example formatted date would appear as "Sun, 06 Nov 1994 08:49:37 GMT"
45      *
46      * @param date the date to format
47      * @return the given date formatting according to RFC 1123
48      */

49     public static String JavaDoc format(Date JavaDoc date) {
50
51         // Compute day number, seconds since beginning of day
52
long s = date.getTime();;
53         if (s >= 0) {
54             // seconds since 1 Jan 1970
55
s /= 1000;
56         } else {
57             // floor(sec/1000)
58
s = (s - 999) / 1000;
59         }
60
61         int dn = (int) (s / 86400);
62         s %= 86400; // positive seconds since beginning of day
63
if (s < 0) {
64             s += 86400;
65             dn--;
66         }
67         dn += 1969 * 365 + 492 - 19 + 4; // days since "1 Jan, year 1"
68

69         // Convert days since 1 Jan, year 1 to year/yearday
70
int y = 400 * (dn / 146097) + 1;
71         int d = dn % 146097;
72         if (d == 146096) {
73             // Last year of 400 is long
74
y += 399;
75             d = 365;
76         } else {
77             y += 100 * (d / 36524);
78             d %= 36524;
79             y += 4 * (d / 1461);
80             d %= 1461;
81             if (d == 1460) {
82                 // Last year out of 4 is long
83
y += 3;
84                 d = 365;
85             } else {
86                 y += d / 365;
87                 d %= 365;
88             }
89         }
90
91         boolean isleap = ((y % 4 == 0) && !(y % 100 == 0)) || (y % 400 == 0);
92
93         // Compute month/monthday from year/yearday
94
if (!isleap && (d >= 59)) {
95             // Skip non-existent Feb 29
96
d++;
97         }
98         if (d >= 60) {
99             // Skip non-existent Feb 30
100
d++;
101         }
102         int mon = ((d % 214) / 61) * 2 + ((d % 214) % 61) / 31;
103         if (d > 213) {
104             mon += 7;
105         }
106         d = ((d % 214) % 61) % 31 + 1;
107
108     // Convert second to hour/min/sec
109
int m = (int) (s / 60);
110     int h = m / 60;
111     m %= 60;
112     s %= 60;
113
114     // Day of week, 0==Sun
115
int w = (dn + 1) % 7;
116
117     /* RFC 1123 date string: "Sun, 06 Nov 1994 08:49:37 GMT" */
118     StringBuffer JavaDoc buff = new StringBuffer JavaDoc(32);
119     buff.append(WEEKDAYS[w]);
120     buff.append(", ");
121     buff.append((char) (d / 10 + '0'));
122     buff.append((char) (d % 10 + '0'));
123     buff.append(' ');
124     buff.append(MONTHS[mon]);
125     buff.append(' ');
126     buff.append(y);
127     buff.append(' ');
128     buff.append((char) (h / 10 + '0'));
129     buff.append((char) (h % 10 + '0'));
130     buff.append(':');
131     buff.append((char) (m / 10 + '0'));
132     buff.append((char) (m % 10 + '0'));
133     buff.append(':');
134     buff.append((char) (s / 10 + '0'));
135     buff.append((char) (s % 10 + '0'));
136     buff.append(" GMT");
137     return buff.toString();
138     }
139 }
140
Popular Tags