JAVA 的 CST,UTC 日期格式的互相转换 和GMT时区
UTC: 世界协调时间
CST: 中央标准时间
GMT: 时区
当UTC时间为0点时,中国CST时间为8点,因为零时区和中国北京时区相差8个时区。
话不多说
先来UTC格式的日期GMT时区写法
/**
* 生成当前的UTC时间戳
*/
private String generateTimestamp() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(new SimpleTimeZone(0, "GMT"));
return df.format(date);
}
输出的格式是UTC格式 比本地的系统时间少八个小时,一般有的验证签名的使用的是这种日期的算法 :
当然也有同学说想要直接减掉本地系统时间的八个小时 来完成GMT的时区差,下面贴出的就是
//UTC格式转GMT时区
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
date = df.parse(resStr);
long retime = date.getTime() - 8 * 60 * 60 * 1000;
String time = df.format(retime);
return time;