Http格林尼治時間和毫秒的相互轉化EEE, dd MMM y HH:mm:ss 'GMT'
阿新 • • 發佈:2019-02-02
前言
寫這個部落格是因為這段時間寫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的極限,所以得到值比預想的要小。