1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.http;
21
22 import java.text.DateFormat;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.Locale;
28 import java.util.TimeZone;
29 import java.util.regex.Pattern;
30
31 /**
32 *
33 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34 */
35 public class DateUtil {
36 /** A thread local is used to protect the DateFormat against concurrent access */
37 private static final ThreadLocal<DateFormat> RFC_1123_FORMAT = new ThreadLocal<DateFormat>() {
38 /**
39 * Initialize the formatter
40 */
41 protected DateFormat initialValue() {
42 DateFormat dateFormat = new SimpleDateFormat(RFC_1123_PATTERN, Locale.US);
43 TimeZone gmtZone = TimeZone.getTimeZone("GMT");
44 dateFormat.setTimeZone(gmtZone);
45
46 return dateFormat;
47 }
48 };
49
50 /** The format of an HTTP date */
51 private static final String RFC_1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
52
53 /** Pattern to find digits only. */
54 private static final Pattern DIGIT_PATTERN = Pattern.compile("^\\d+$");
55
56 /**
57 * Returns the current date as String
58 *
59 * @return Current Date as String, in the format <i>EEE, dd MMM yyyy HH:mm:ss zzz</i>
60 */
61 public static String getCurrentAsString() {
62 return RFC_1123_FORMAT.get().format(new Date());
63 }
64
65 /**
66 * Translate a given date <code>String</code> in the <em>RFC 1123</em>
67 * format to a <code>long</code> representing the number of milliseconds
68 * since epoch.
69 *
70 * @param dateString a date <code>String</code> in the <em>RFC 1123</em>
71 * format.
72 * @return the parsed <code>Date</code> in milliseconds.
73 */
74 private static long parseDateStringToMilliseconds(final String dateString) {
75
76 try {
77 return RFC_1123_FORMAT.get().parse(dateString).getTime();
78 } catch (final ParseException e) {
79 return 0;
80 }
81 }
82
83 /**
84 * Parse a given date <code>String</code> to a <code>long</code>
85 * representation of the time. Where the provided value is all digits the
86 * value is returned as a <code>long</code>, otherwise attempt is made to
87 * parse the <code>String</code> as a <em>RFC 1123</em> date.
88 *
89 * @param dateValue the value to parse.
90 * @return the <code>long</code> value following parse, or zero where not
91 * successful.
92 */
93 public static long parseToMilliseconds(final String dateValue) {
94
95 long ms = 0;
96
97 if (DIGIT_PATTERN.matcher(dateValue).matches()) {
98 ms = Long.parseLong(dateValue);
99 } else {
100 ms = parseDateStringToMilliseconds(dateValue);
101 }
102
103 return ms;
104 }
105
106 /**
107 * Converts a millisecond representation of a date to a
108 * <code>RFC 1123</code> formatted <code>String</code>.
109 *
110 * @param dateValue the <code>Date</code> represented as milliseconds.
111 * @return a <code>String</code> representation of the date.
112 */
113 public static String parseToRFC1123(final long dateValue) {
114 final Calendar calendar = Calendar.getInstance();
115 calendar.setTimeInMillis(dateValue);
116
117 return RFC_1123_FORMAT.get().format(calendar.getTime());
118 }
119
120 /**
121 * Convert a given <code>Date</code> object to a <code>RFC 1123</code>
122 * formatted <code>String</code>.
123 *
124 * @param date the <code>Date</code> object to convert
125 * @return a <code>String</code> representation of the date.
126 */
127 public static String getDateAsString(Date date) {
128 return RFC_1123_FORMAT.get().format(date);
129 }
130 }