程序员人生 网站导航

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

栏目:框架设计时间:2016-06-27 08:40:45

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

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

版权声明:转载请注明本文转自严振杰的CSDN博客: http://blog.csdn.net/yanzhenjie1003

QQ交换群1:46523908
QQ交换群2:46505645
群资源非常有限,请先加群1,如果群1满再加群2,谢谢。

前言

  写这个博客是由于这段时间写NoHttp时遇到格式话Http响应头和要求头中和Data有关的字段时遇到的1些问题,最后总结了1下算法。
  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); }

合起来写成1个类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; } }

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


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

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐