All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.net.http.common.codec.DateParser Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.http.common.codec;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * ThreadLocal data parsers for HTTP style dates
 */
public class DateParser {
    final static String[] DATE_RECEIVE_FMT = {
            "EEE, dd MMM yyyy HH:mm:ss zzz", "EEE, dd-MMM-yy HH:mm:ss",
            "EEE MMM dd HH:mm:ss yyyy",

            "EEE, dd MMM yyyy HH:mm:ss", "EEE dd MMM yyyy HH:mm:ss zzz", "EEE dd MMM yyyy HH:mm:ss",
            "EEE MMM dd yyyy HH:mm:ss zzz", "EEE MMM dd yyyy HH:mm:ss", "EEE MMM-dd-yyyy HH:mm:ss zzz",
            "EEE MMM-dd-yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss zzz", "dd MMM yyyy HH:mm:ss", "dd-MMM-yy HH:mm:ss zzz",
            "dd-MMM-yy HH:mm:ss", "MMM dd HH:mm:ss yyyy zzz", "MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz",
            "EEE, MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy", "EEE, dd-MMM-yy HH:mm:ss zzz",
            "EEE dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss"};
    private static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");
    private static final ThreadLocal DATE_PARSER = ThreadLocal.withInitial(DateParser::new);

    static {
        GMT_TIME_ZONE.setID("GMT");
    }

    final SimpleDateFormat[] DATE_RECEIVE = new SimpleDateFormat[DATE_RECEIVE_FMT.length];

    public static long parseDate(String date) {
        return DATE_PARSER.get().parse(date);
    }

    private long parse(final String dateVal) {
        for (int i = 0; i < DATE_RECEIVE.length; i++) {
            if (DATE_RECEIVE[i] == null) {
                DATE_RECEIVE[i] = new SimpleDateFormat(DATE_RECEIVE_FMT[i], Locale.US);
                DATE_RECEIVE[i].setTimeZone(GMT_TIME_ZONE);
            }

            try {
                Date date = (Date) DATE_RECEIVE[i].parseObject(dateVal);
                return date.getTime();
            } catch (Exception ignored) {
            }
        }

        if (dateVal.endsWith(" GMT")) {
            final String val = dateVal.substring(0, dateVal.length() - 4);

            for (SimpleDateFormat element : DATE_RECEIVE) {
                try {
                    Date date = (Date) element.parseObject(val);
                    return date.getTime();
                } catch (Exception ignored) {
                }
            }
        }
        return -1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy