Java Parse Date Pattern YYYY parseHeaderDate(String headerValue)

Here you can find the source of parseHeaderDate(String headerValue)

Description

Get a header date value as Date

License

Apache License

Parameter

Parameter Description
headerValue Header

Return

Date or null if header is null or invalid

Declaration

public static Date parseHeaderDate(String headerValue) 

Method Source Code

//package com.java2s;
/*/*w w  w  .  j  a  v a  2s  .  c  o m*/
 * Copyright 2016-2017 Axioma srl.
 * 
 * Licensed 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.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

import java.util.TimeZone;

public class Main {
    /**
     * Request header date formats as specified in the HTTP RFC
     * @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
     */
    public static final String[] DATE_FORMATS = new String[] { "EEE, dd MMM yyyy HH:mm:ss zzz",
            "EEE, dd-MMM-yy HH:mm:ss zzz", "EEE MMM dd HH:mm:ss yyyy" };

    /**
     * Get a header date value as {@link Date}
     * @param headerValue Header
     * @return Date or <code>null</code> if header is null or invalid
     */
    public static Date parseHeaderDate(String headerValue) {
        if (headerValue != null && headerValue.length() >= 3) {
            for (String dateFormat : DATE_FORMATS) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
                try {
                    return simpleDateFormat.parse(headerValue);
                } catch (@SuppressWarnings("unused") ParseException ex) {
                    // ignore
                }
            }
        }
        return null;
    }
}

Related

  1. parseFormattedTime(String formattedTime)
  2. parseFTPDate(String dateStr)
  3. parseFullDate(String paramName, String dateStr)
  4. parseFullTime(String str)
  5. parseGeneralizedTimeAsDate(String generalizedTime)
  6. parseHHMMSS(long time)
  7. parseInsituFileNameDateFormat(String timeString)
  8. parseLastModifDate(final File file)
  9. parseLogstashDate(final String date_string)