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          return DateUtil.RFC_1123_FORMAT.format(new Date()); //NOPMD
    49      }
    50
    51      /**
    52       * Translate a given date <code>String</code> in the <em>RFC 1123</em>
    53       * format to a <code>long</code> representing the number of milliseconds
    54       * since epoch.
    55       *
    56       * @param dateString a date <code>String</code> in the <em>RFC 1123</em>
    57       *            format.
    58       * @return the parsed <code>Date</code> in milliseconds.
    59       */
    60      private static long parseDateStringToMilliseconds(final String dateString) {
    61
    62          try {
    63              return DateUtil.RFC_1123_FORMAT.parse(dateString).getTime(); //NOPMD
    64          } catch (final ParseException e) {
    65              return 0;
    66          }
    67      }
    68
    69      /**
    70       * Parse a given date <code>String</code> to a <code>long</code>
    71       * representation of the time. Where the provided value is all digits the
    72       * value is returned as a <code>long</code>, otherwise attempt is made to
    73       * parse the <code>String</code> as a <em>RFC 1123</em> date.
    74       *
    75       * @param dateValue the value to parse.
    76       * @return the <code>long</code> value following parse, or zero where not
    77       *         successful.
    78       */
    79      public static long parseToMilliseconds(final String dateValue) {
    80
    81          long ms = 0;
    82
    83          if (DateUtil.DIGIT_PATTERN.matcher(dateValue).matches()) {
    84              ms = Long.parseLong(dateValue);
    85          } else {
    86              ms = parseDateStringToMilliseconds(dateValue);
    87          }
    88
    89          return ms;
    90      }
    91
    92      /**
    93       * Converts a millisecond representation of a date to a
    94       * <code>RFC 1123</code> formatted <code>String</code>.
    95       *
    96       * @param dateValue the <code>Date</code> represented as milliseconds.
    97       * @return a <code>String</code> representation of the date.
    98       */
    99      public static String parseToRFC1123(final long dateValue) {
    100
    101         final Calendar calendar = Calendar.getInstance();
    102         calendar.setTimeInMillis(dateValue);
    103
    104         return DateUtil.RFC_1123_FORMAT.format(calendar.getTime()); //NOPMD
    105     }
    106
    107     /**
    108      * Convert a given <code>Date</code> object to a <code>RFC 1123</code>
    109      * formatted <code>String</code>.
    110      *
    111      * @param date the <code>Date</code> object to convert
    112      * @return a <code>String</code> representation of the date.
    113      */
    114     public static String getDateAsString(Date date) {
    115         return RFC_1123_FORMAT.format(date); //NOPMD
    116     }
    117
    118 }