View Javadoc
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   * An utility class for Dates manipulations
33   * 
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class DateUtil {
37      private static final Locale LOCALE = Locale.US;
38      private static final TimeZone GMT_ZONE;
39      private static final String RFC_1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
40      private static final DateFormat RFC_1123_FORMAT;
41  
42      /** Pattern to find digits only. */
43      private static final Pattern DIGIT_PATTERN = Pattern.compile("^\\d+$");
44  
45      static {
46          RFC_1123_FORMAT = new SimpleDateFormat(DateUtil.RFC_1123_PATTERN, DateUtil.LOCALE);
47          GMT_ZONE = TimeZone.getTimeZone("GMT");
48          DateUtil.RFC_1123_FORMAT.setTimeZone(DateUtil.GMT_ZONE);
49      }
50  
51      private DateUtil() {
52      }
53  
54      /**
55       * @return The current date as a string
56       */
57      public static String getCurrentAsString() {
58          synchronized(DateUtil.RFC_1123_FORMAT) {
59              return DateUtil.RFC_1123_FORMAT.format(new Date()); //NOPMD
60          }
61      }
62  
63      /**
64       * Translate a given date <code>String</code> in the <em>RFC 1123</em>
65       * format to a <code>long</code> representing the number of milliseconds
66       * since epoch.
67       * 
68       * @param dateString a date <code>String</code> in the <em>RFC 1123</em> format.
69       * @return the parsed <code>Date</code> in milliseconds.
70       */
71      private static long parseDateStringToMilliseconds(String dateString) {
72          try {
73              synchronized (DateUtil.RFC_1123_FORMAT) {
74                  return DateUtil.RFC_1123_FORMAT.parse(dateString).getTime(); //NOPMD
75              }
76          } catch (ParseException e) {
77              return 0;
78          }
79      }
80  
81      /**
82       * Parse a given date <code>String</code> to a <code>long</code>
83       * representation of the time. Where the provided value is all digits the
84       * value is returned as a <code>long</code>, otherwise attempt is made to
85       * parse the <code>String</code> as a <em>RFC 1123</em> date.
86       * 
87       * @param dateValue the value to parse.
88       * @return the <code>long</code> value following parse, or zero where not successful.
89       */
90      public static long parseToMilliseconds(String dateValue) {
91          if (DateUtil.DIGIT_PATTERN.matcher(dateValue).matches()) {
92              return Long.parseLong(dateValue);
93          } else {
94              return parseDateStringToMilliseconds(dateValue);
95          }
96      }
97  
98      /**
99       * Converts a millisecond representation of a date to a
100      * <code>RFC 1123</code> formatted <code>String</code>.
101      * 
102      * @param dateValue the <code>Date</code> represented as milliseconds.
103      * @return a <code>String</code> representation of the date.
104      */
105     public static String parseToRFC1123(long dateValue) {
106 
107         Calendar calendar = Calendar.getInstance();
108         calendar.setTimeInMillis(dateValue);
109 
110         synchronized (DateUtil.RFC_1123_FORMAT) {
111             return DateUtil.RFC_1123_FORMAT.format(calendar.getTime()); //NOPMD
112         }
113     }
114 
115     /**
116      * Convert a given <code>Date</code> object to a <code>RFC 1123</code>
117      * formatted <code>String</code>.
118      * 
119      * @param date the <code>Date</code> object to convert
120      * @return a <code>String</code> representation of the date.
121      */
122     public static String getDateAsString(Date date) {
123         synchronized (DateUtil.RFC_1123_FORMAT) {
124             return RFC_1123_FORMAT.format(date); //NOPMD
125         }
126     }
127 }