KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > buf > DateTool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You 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 implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.buf;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.FieldPosition JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.TimeZone JavaDoc;
27
28 import org.apache.tomcat.util.res.StringManager;
29
30 /**
31  * Common place for date utils.
32  *
33  * @deprecated Will be replaced with a more efficient impl, based on
34  * FastDateFormat, with an API using less objects.
35  * @author dac@eng.sun.com
36  * @author Jason Hunter [jch@eng.sun.com]
37  * @author James Todd [gonzo@eng.sun.com]
38  * @author Costin Manolache
39  */

40 public class DateTool {
41
42     /** US locale - all HTTP dates are in english
43      */

44     private final static Locale JavaDoc LOCALE_US = Locale.US;
45
46     /** GMT timezone - all HTTP dates are on GMT
47      */

48     public final static TimeZone JavaDoc GMT_ZONE = TimeZone.getTimeZone("GMT");
49
50     /** format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT"
51      */

52     public final static String JavaDoc RFC1123_PATTERN =
53         "EEE, dd MMM yyyy HH:mm:ss z";
54
55     // format for RFC 1036 date string -- "Sunday, 06-Nov-94 08:49:37 GMT"
56
public final static String JavaDoc rfc1036Pattern =
57         "EEEEEEEEE, dd-MMM-yy HH:mm:ss z";
58
59     // format for C asctime() date string -- "Sun Nov 6 08:49:37 1994"
60
public final static String JavaDoc asctimePattern =
61         "EEE MMM d HH:mm:ss yyyy";
62
63     /** Pattern used for old cookies
64      */

65     private final static String JavaDoc OLD_COOKIE_PATTERN = "EEE, dd-MMM-yyyy HH:mm:ss z";
66
67     /** DateFormat to be used to format dates. Called from MessageBytes
68      */

69     private final static DateFormat JavaDoc rfc1123Format =
70     new SimpleDateFormat JavaDoc(RFC1123_PATTERN, LOCALE_US);
71     
72     /** DateFormat to be used to format old netscape cookies
73     Called from ServerCookie
74      */

75     private final static DateFormat JavaDoc oldCookieFormat =
76     new SimpleDateFormat JavaDoc(OLD_COOKIE_PATTERN, LOCALE_US);
77     
78     private final static DateFormat JavaDoc rfc1036Format =
79     new SimpleDateFormat JavaDoc(rfc1036Pattern, LOCALE_US);
80     
81     private final static DateFormat JavaDoc asctimeFormat =
82     new SimpleDateFormat JavaDoc(asctimePattern, LOCALE_US);
83     
84     static {
85     rfc1123Format.setTimeZone(GMT_ZONE);
86     oldCookieFormat.setTimeZone(GMT_ZONE);
87     rfc1036Format.setTimeZone(GMT_ZONE);
88     asctimeFormat.setTimeZone(GMT_ZONE);
89     }
90  
91     private static String JavaDoc rfc1123DS;
92     private static long rfc1123Sec;
93
94     private static StringManager sm =
95         StringManager.getManager("org.apache.tomcat.util.buf.res");
96
97     // Called from MessageBytes.getTime()
98
static long parseDate( MessageBytes value ) {
99         return parseDate( value.toString());
100     }
101
102     // Called from MessageBytes.setTime
103
/**
104      */

105     public static String JavaDoc format1123( Date JavaDoc d ) {
106     String JavaDoc dstr=null;
107     synchronized(rfc1123Format) {
108         dstr = format1123(d, rfc1123Format);
109     }
110     return dstr;
111     }
112
113     public static String JavaDoc format1123( Date JavaDoc d,DateFormat JavaDoc df ) {
114         long dt = d.getTime() / 1000;
115         if ((rfc1123DS != null) && (dt == rfc1123Sec))
116             return rfc1123DS;
117         rfc1123DS = df.format( d );
118         rfc1123Sec = dt;
119         return rfc1123DS;
120     }
121
122
123     // Called from ServerCookie
124
/**
125      */

126     public static void formatOldCookie( Date JavaDoc d, StringBuffer JavaDoc sb,
127                       FieldPosition JavaDoc fp )
128     {
129     synchronized(oldCookieFormat) {
130         oldCookieFormat.format( d, sb, fp );
131     }
132     }
133
134     // Called from ServerCookie
135
public static String JavaDoc formatOldCookie( Date JavaDoc d )
136     {
137     String JavaDoc ocf=null;
138     synchronized(oldCookieFormat) {
139         ocf= oldCookieFormat.format( d );
140     }
141     return ocf;
142     }
143
144     
145     /** Called from HttpServletRequest.getDateHeader().
146     Not efficient - but not very used.
147      */

148     public static long parseDate( String JavaDoc dateString ) {
149     DateFormat JavaDoc [] format = {rfc1123Format,rfc1036Format,asctimeFormat};
150     return parseDate(dateString,format);
151     }
152     public static long parseDate( String JavaDoc dateString, DateFormat JavaDoc []format ) {
153     Date JavaDoc date=null;
154     for(int i=0; i < format.length; i++) {
155         try {
156         date = format[i].parse(dateString);
157         return date.getTime();
158         } catch (ParseException JavaDoc e) { }
159         catch (StringIndexOutOfBoundsException JavaDoc e) { }
160     }
161     String JavaDoc msg = sm.getString("httpDate.pe", dateString);
162     throw new IllegalArgumentException JavaDoc(msg);
163     }
164
165 }
166
Popular Tags