content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  
  package com.aliyun.oss.common.utils;
  
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Locale;
  import java.util.SimpleTimeZone;
  
  /**
   * A simple utility class for date formating.
   */
  public class DateUtil {
  
      // RFC 822 Date Format
      private static final String RFC822_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
  
      // ISO 8601 format
      private static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
  
      // Alternate ISO 8601 format without fractional seconds
      private static final String ALTERNATIVE_ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  
      /**
       * Formats Date to GMT string.
       * @param date
       *          a {@link Date} instance.
       * @return a RFC 822 date format string
       */
      public static String formatRfc822Date(Date date) {
          return getRfc822DateFormat().format(date);
      }
  
      /**
       * Parses a GMT-format string.
       * @param dateString
       *          a RFC 822 date format string.
       * @return a {@link Date} instance.
       * @throws ParseException
       *         if an parsing error occurs
       */
      public static Date parseRfc822Date(String dateString) throws ParseException {
          return getRfc822DateFormat().parse(dateString);
      }
  
      private static DateFormat getRfc822DateFormat() {
          SimpleDateFormat rfc822DateFormat = new SimpleDateFormat(RFC822_DATE_FORMAT, Locale.US);
          rfc822DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
  
          return rfc822DateFormat;
      }
  
      public static String formatIso8601Date(Date date) {
          return getIso8601DateFormat().format(date);
      }
  
      public static String formatAlternativeIso8601Date(Date date) {
          return getAlternativeIso8601DateFormat().format(date);
      }
  
      /**
       * Parse a date string in the format of ISO 8601.
       * 
       * @param dateString
       *          a ISO 8601 date format string.
       * @return a {@link Date} instance.
       * @throws ParseException
       *         if an parsing error occurs
       */
      public static Date parseIso8601Date(String dateString) throws ParseException {
          try {
              return getIso8601DateFormat().parse(dateString);
          } catch (ParseException e) {
              return getAlternativeIso8601DateFormat().parse(dateString);
          }
      }
  
      private static DateFormat getIso8601DateFormat() {
          SimpleDateFormat df = new SimpleDateFormat(ISO8601_DATE_FORMAT, Locale.US);
          df.setTimeZone(new SimpleTimeZone(0, "GMT"));
          return df;
      }
  
      private static DateFormat getAlternativeIso8601DateFormat() {
          SimpleDateFormat df = new SimpleDateFormat(ALTERNATIVE_ISO8601_DATE_FORMAT, Locale.US);
          df.setTimeZone(new SimpleTimeZone(0, "GMT"));
          return df;
      }
  }
  