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