2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.Locale;
24 import java.util.TimeZone;
27 * ThreadLocal data parsers for HTTP style dates
32 private static final TimeZone __GMT = TimeZone.getTimeZone("GMT");
38 final static String __dateReceiveFmt[] =
40 "EEE, dd MMM yyyy HH:mm:ss zzz",
41 "EEE, dd-MMM-yy HH:mm:ss",
42 "EEE MMM dd HH:mm:ss yyyy",
44 "EEE, dd MMM yyyy HH:mm:ss", "EEE dd MMM yyyy HH:mm:ss zzz",
45 "EEE dd MMM yyyy HH:mm:ss", "EEE MMM dd yyyy HH:mm:ss zzz", "EEE MMM dd yyyy HH:mm:ss",
46 "EEE MMM-dd-yyyy HH:mm:ss zzz", "EEE MMM-dd-yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss zzz",
47 "dd MMM yyyy HH:mm:ss", "dd-MMM-yy HH:mm:ss zzz", "dd-MMM-yy HH:mm:ss", "MMM dd HH:mm:ss yyyy zzz",
48 "MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz",
49 "EEE, MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy", "EEE, dd-MMM-yy HH:mm:ss zzz",
50 "EEE dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss",
53 public static long parseDate(String date)
55 return __dateParser.get().parse(date);
58 private static final ThreadLocal<DateParser> __dateParser =new ThreadLocal<DateParser>()
61 protected DateParser initialValue()
63 return new DateParser();
67 final SimpleDateFormat _dateReceive[]= new SimpleDateFormat[__dateReceiveFmt.length];
69 private long parse(final String dateVal)
71 for (int i = 0; i < _dateReceive.length; i++)
73 if (_dateReceive[i] == null)
75 _dateReceive[i] = new SimpleDateFormat(__dateReceiveFmt[i], Locale.US);
76 _dateReceive[i].setTimeZone(__GMT);
81 Date date = (Date) _dateReceive[i].parseObject(dateVal);
82 return date.getTime();
84 catch (java.lang.Exception e)
90 if (dateVal.endsWith(" GMT"))
92 final String val = dateVal.substring(0, dateVal.length() - 4);
94 for (SimpleDateFormat element : _dateReceive)
98 Date date = (Date) element.parseObject(val);
99 return date.getTime();
101 catch (java.lang.Exception e)