标签:
GMT时间是格林尼治标准时间,CST时间是指包括中国,美国,巴西,澳大利亚四个时区的时间。
在javascript中默认CST是指美国中部时间,倘若在javascript中GMT转换CST则两者相差14个小时。在java后台中默认的是北京时间,GMT转换成CST则相差8个小时。各个地方用CST时间得到的可能会有所不同,所以为了避免编程错误,一般使用GMT时间。以下是从其他地方找到的三种转换方式。
Date date = new Date(); date.toGMTString();因此方法在高版本的JDK中已经失效,不推荐使用。
DateFormat cstFormat = new SimpleDateFormat(); DateFormat gmtFormat = new SimpleDateFormat(); TimeZone gmtTime = TimeZone.getTimeZone("GMT"); TimeZone cstTime = TimeZone.getTimeZone("CST"); cstFormat.setTimeZone(gmtTime); gmtFormat.setTimeZone(cstTime); System.out.println("GMT Time: " + cstFormat.format(date)); System.out.println("CST Time: " + gmtFormat.format(date));得到的格式很单调,只有年月日+上下午+时分。感觉不太好用,不推荐使用
public Date getCST(String strGMT) throws ParseException { DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH); return df.parse(strGMT); } public String getGMT(Date dateCST) { DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH); df.setTimeZone(TimeZone.getTimeZone("GMT")); // modify Time Zone. return(df.format(dateCST)); }一般我们的web请求的请求头中的Date格式类似于:Thu, 02 Jul 2015 05:49:30 GMT ,可以相应的把上面的格式调整为:
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);这种方式可以灵活控制时间的格式,信息量较全,推荐使用。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/js_sky/article/details/46727053