GMT和CST的转换
来源:互联网 发布:playclub陈诗涵mod数据 编辑:程序博客网 时间:2024/05/24 06:50
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);
这种方式可以灵活控制时间的格式,信息量较全,推荐使用。 - 下面是三种方式测试的结果:
1 0
- GMT和CST的转换
- CST 和GMT时间的区别
- CST和GMT时间的区别
- CST和GMT时间的区别
- CST和GMT时间的区别
- CST和GMT时间的区别
- CST和GMT时间的区别
- CST和GMT时间的区别
- CST和GMT时间的区别
- JavaScript时间转换CST到GMT
- java GMT时间转换为CST时间
- 获取 GMT 时间对应的 CST 时间
- C#将GMT时间格式转换为CST时间格式
- GMT to CST
- GMT,UTC,CST
- GMT,UTC,CST
- java new Date() 变成GMT&& GMT时间与CST时间转换
- GMT CST UTC 时间一览
- 【Leetcode Algorithm】Contains Duplicate II
- [Python]cherrypy环境下生成csv文件,提供下载
- C语言中关于进程的函数
- centos 7 密码忘记 如何进入修改
- 深入理解PHP之数组(遍历顺序)
- GMT和CST的转换
- linux下weblogic11g安装成功后,启动报错Getting boot identity from user
- 链式队列
- 防SQL注入
- 学习网址
- 使用系统下载管理类DownloadManager
- 【Leetcode Algorithm】Rectangle Area
- 关于交叉编译器默认体系结构的问题
- 攻克GRE阅读难关的方法