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
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值