Http格林尼治时间和毫秒的相互转化EEE, dd MMM y HH:mm:ss 'GMT'

NoHttp开源地址:https://github.com/yanzhenjie/NoHttp

NoHttp详细使用文档已发布,你想知道的全都有,请点我移步!

版权声明:转载请注明本文转自严振杰的博客: http://blog.yanzhenjie.com

前言

  写这个博客是因为这段时间写NoHttp时遇到格式话Http响应头和请求头中和Data有关的字段时遇到的一些问题,最后总结了一下算法。
  2个概念明确下:
  1. 格林尼治时间(有的出版局叫格林威治Greenwhich)的格式:”EEE, dd MMM y HH:mm:ss ‘GMT’”
  2. 毫秒格式:1464709616971


格林尼治(EEE, dd MMM y HH:mm:ss ‘GMT’)转化为毫秒

public static final String FORMAT_HTTP_DATA = "EEE, dd MMM y HH:mm:ss 'GMT'";

public static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");

/**
 * Parsing the TimeZone of time in milliseconds.
 *
 * @param gmtTime GRM Time, Format such as: {@value #FORMAT_HTTP_DATA}.
 * @return The number of milliseconds from 1970.1.1.
 * @throws ParseException if an error occurs during parsing.
 */
public static long parseGMTToMillis(String gmtTime) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_HTTP_DATA, Locale.US);
    formatter.setTimeZone(GMT_TIME_ZONE);
    Date date = formatter.parse(gmtTime);
    return date.getTime();
}

毫秒转化为格林尼治(EEE, dd MMM y HH:mm:ss ‘GMT’)

public static final String FORMAT_HTTP_DATA = "EEE, dd MMM y HH:mm:ss 'GMT'";

public static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");

/**
 * Parsing the TimeZone of time from milliseconds.
 *
 * @param milliseconds the number of milliseconds from 1970.1.1.
 * @return GRM Time, Format such as: {@value #FORMAT_HTTP_DATA}.
 */
public static String formatMillisToGMT(long milliseconds) {
    Date date = new Date(milliseconds);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_HTTP_DATA, Locale.US);
    simpleDateFormat.setTimeZone(GMT_TIME_ZONE);
    return simpleDateFormat.format(date);
}

合起来写成一个类HttpDate

public final class HttpDateTime {

    public static final String FORMAT_HTTP_DATA = "EEE, dd MMM y HH:mm:ss 'GMT'";

    public static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");

    /**
     * Parsing the TimeZone of time in milliseconds.
     *
     * @param gmtTime GRM Time, Format such as: {@value #FORMAT_HTTP_DATA}.
     * @return The number of milliseconds from 1970.1.1.
     * @throws ParseException if an error occurs during parsing.
     */
    public static long parseGMTToMillis(String gmtTime) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_HTTP_DATA, Locale.US);
        formatter.setTimeZone(GMT_TIME_ZONE);
        Date date = formatter.parse(gmtTime);
        return date.getTime();
    }

    /**
     * Parsing the TimeZone of time from milliseconds.
     *
     * @param milliseconds the number of milliseconds from 1970.1.1.
     * @return GRM Time, Format such as: {@value #FORMAT_HTTP_DATA}.
     */
    public static String formatMillisToGMT(long milliseconds) {
        Date date = new Date(milliseconds);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_HTTP_DATA, Locale.US);
        simpleDateFormat.setTimeZone(GMT_TIME_ZONE);
        return simpleDateFormat.format(date);
    }

    /**
     * Returned the local number of milliseconds after 100.
     *
     * @return Long format time.
     */
    public static long getMaxExpiryMillis() {
        return System.currentTimeMillis() + 1000L * 60L * 60L * 24L * 365L * 100L;
    }

}

  最后一个方法是得到一个100年的毫秒时间,有的同学算到的是错的,问题出在有的数字后面没有L,所以超出了int的极限,所以得到值比预想的要小。


欢迎大家关注Android开源网络框架NoHttp:https://github.com/yanzhenjie/NoHttp
在线直播视频和代码下载:http://pan.baidu.com/s/1miEOtwG
  

你可能感兴趣的:(Android)