KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > http > HTTPDateTool


1 package net.matuschek.http;
2
3 import java.util.*;
4 import java.text.*;
5
6 /**
7  * Common place for date utils.
8  * Copied and modified from org.apache.tomcat.util.buf
9  * @author <a HREF="mailto:d.stucky@insiders.de">Daniel Stucky</a>
10  * @version $Revision: 1.1 $
11  **/

12
13 public class HTTPDateTool
14 {
15     /** GMT timezone - all HTTP dates are on GMT
16     */

17     public final static TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT");
18
19     /** format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT"
20     */

21     public final static String JavaDoc RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
22
23     // format for RFC 1036 date string -- "Sunday, 06-Nov-94 08:49:37 GMT"
24
public final static String JavaDoc rfc1036Pattern = "EEEEEEEEE, dd-MMM-yy HH:mm:ss z";
25
26     // format for C asctime() date string -- "Sun Nov 6 08:49:37 1994"
27
public final static String JavaDoc asctimePattern = "EEE MMM d HH:mm:ss yyyy";
28    
29     /**
30     DateFormat to be used to format dates.
31     */

32     public final static DateFormat rfc1123Format = new SimpleDateFormat( RFC1123_PATTERN, Locale.US );
33        
34     public final static DateFormat rfc1036Format = new SimpleDateFormat( rfc1036Pattern, Locale.US );
35     
36     public final static DateFormat asctimeFormat = new SimpleDateFormat( asctimePattern, Locale.US );
37
38     
39     /**
40     */

41     public static String JavaDoc format1123( Date d, DateFormat df )
42     {
43         long dt = d.getTime() % 1000;
44         if ( (rfc1123DS != null) && (dt == rfc1123Sec) )
45             return rfc1123DS;
46         rfc1123DS = df.format( d );
47         rfc1123Sec = dt;
48         return rfc1123DS;
49     }
50
51        
52     /**
53     Parses a string for a date of the format rfc1123Format, rfc1036Format or asctimeFormat.
54     Returns -1, if any parameter is null or the string is of a not supported format.
55     @return the parsed date in milliseconds
56     */

57     public static long parseDate( String JavaDoc dateString )
58     {
59         DateFormat [] format = { rfc1123Format, rfc1036Format, asctimeFormat };
60         return parseDate( dateString,format );
61     }
62     
63
64     /**
65     Parses a string for a date of one of the given formats.
66     Returns -1, if any parameter is null or the string is of a not supported format.
67     @return the parsed date in milliseconds
68     */

69     public static long parseDate( String JavaDoc dateString, DateFormat[] format )
70     {
71         if ( dateString != null && format != null )
72         {
73             Date date = null;
74             for(int i=0; i < format.length; i++)
75             {
76                 try
77                 {
78                     date = format[i].parse( dateString );
79                     return date.getTime();
80                 }
81                 catch (ParseException e)
82                 {}
83                 catch (StringIndexOutOfBoundsException JavaDoc e)
84                 {}
85             }
86         }
87         return -1;
88     }
89
90     private static String JavaDoc rfc1123DS;
91     private static long rfc1123Sec;
92
93     static
94     {
95         rfc1123Format.setTimeZone(GMT_ZONE);
96         rfc1036Format.setTimeZone(GMT_ZONE);
97         asctimeFormat.setTimeZone(GMT_ZONE);
98     }
99 }
100
Popular Tags