歡迎大家關(guān)注Android開源網(wǎng)絡框架NoHttp:https://github.com/yanzhenjie/NoHttp
在線直播視頻和代碼下載:http://pan.baidu.com/s/1miEOtwG
版權(quán)聲明:轉(zhuǎn)載請注明本文轉(zhuǎn)自嚴振杰的CSDN博客: http://blog.csdn.net/yanzhenjie1003
QQ交換群1:46523908
QQ交換群2:46505645
群資源非常有限,請先加群1,如果群1滿再加群2,謝謝。
寫這個博客是由于這段時間寫NoHttp時遇到格式話Http響應頭和要求頭中和Data有關(guān)的字段時遇到的1些問題,最后總結(jié)了1下算法。
2個概念明確下:
1. 格林尼治時間(有的出版局叫格林威治Greenwhich)的格式:”EEE, dd MMM y HH:mm:ss ‘GMT’”
2. 毫秒格式:1464709616971
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();
}
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);
}
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年的毫秒時間,有的同學算到的是錯的,問題出在有的數(shù)字后面沒有L,所以超越了int的極限,所以得到值比料想的要小。
歡迎大家關(guān)注Android開源網(wǎng)絡框架NoHttp:https://github.com/yanzhenjie/NoHttp
在線直播視頻和代碼下載:http://pan.baidu.com/s/1miEOtwG