package com.jscape.inet.http.protocol.marshaling;

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

/**
 * @author Alex Usun
 */
public class DateFormatRfc1123 {

    private static final String DATE_TEMPLATE = "EEE, dd MMM yyyy HH:mm:ss zzz";

    public static String format(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TEMPLATE, Locale.ENGLISH);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

        return dateFormat.format(date);
    }

    public static Date parse(String data) {
        if (data == null) {
            return new Date();
        }

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TEMPLATE, Locale.ENGLISH);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

            return dateFormat.parse(data);
        } catch (Exception e) {
            return new Date();
        }
    }

    private DateFormatRfc1123() {
        // empty
    }
}
