KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > HttpDateParser


1 /**
2  * Copyright (C) 2004 Manfred Andres
3  * Created: 18.07.2004 (10:20:04)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19 package freecs.util;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.TimeZone JavaDoc;
23
24 import freecs.Server;
25
26
27 /**
28  * @author Manfred Andres
29  *
30  * TODO To change the template for this generated type comment go to
31  * Window - Preferences - Java - Code Generation - Code and Comments
32  */

33 public class HttpDateParser {
34     public static final String JavaDoc DP_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
35     public static final String JavaDoc DP_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
36     public static final String JavaDoc DP_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
37
38     public static final SimpleDateFormat JavaDoc[] formats = {
39             HttpDateParser.constructSdf(DP_RFC1123),
40             HttpDateParser.constructSdf(DP_RFC1036),
41             HttpDateParser.constructSdf(DP_ASCTIME) };
42
43     /**
44      * @param pattern for parsing the date
45      * @return
46      */

47     private static SimpleDateFormat JavaDoc constructSdf(String JavaDoc pattern) {
48         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc (pattern, Locale.US);
49         sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
50         return sdf;
51     }
52
53     
54     public static long parseDate(String JavaDoc dateStrg) {
55         if (dateStrg == null)
56             return -1;
57         for (int i = 0; i<formats.length; i++) {
58             try {
59                 long val = formats[i].parse(dateStrg).getTime();
60                 Server.log ("HTTP-Date-Parser", "time parsed: " + val, Server.MSG_STATE, Server.LVL_MAJOR);
61                 return val;
62             } catch (Exception JavaDoc pe) {
63                 continue;
64             }
65         }
66         Server.log("HTTP-Date-Parser", "unable to parse date: " + dateStrg, Server.MSG_STATE, Server.LVL_MAJOR);
67         return -1;
68     }
69
70     private HttpDateParser() { }
71 }
72
Popular Tags