首页IT科技java时间转化为数字(Java处理时间格式CST和GMT转换)

java时间转化为数字(Java处理时间格式CST和GMT转换)

时间2025-07-29 11:22:08分类IT科技浏览6712
导读:前言 在编程中处理日期格式时,通常会遇到带CST或GMT的时间格式,它们代表什么,如何转换呢?...

前言

在编程中处理日期格式时              ,通常会遇到带CST或GMT的时间格式                    ,它们代表什么       ,如何转换呢?

概念

CST和GMT时间示例如下:

Mon Oct 26 15:19:15 CST 2022

Thu, 22 Sep 2022 09:41:01 GMT

CST

这个代号缩写              ,并不是一个统一标准                    ,目前       ,可以同时代表如下 4 个不同版本的时区概念(要根据上下文语义加以区分):

1)China Standard Time 中国标准时区 (UTC+8)

2)Cuba Standard Time 古巴标准时区 (UTC-4)

3)Central Standard Time (USA) 美国中央时区 (UTC-6)

4)Central Standard Time (Australia) 澳大利亚中央时区(UTC+9)

GMT

格林尼治时间(另有格林威治时间一说)

转换处理

本地时间为CST格式时间

CST格式字符串转换成yyyy-MM-dd HH:mm:ss格式的时间

代码:

public static void main(String[] args) throws ParseException { String dateStr = "Mon Oct 26 22:22:22 CST 2022"; DateFormat cst = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateFormat gmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); Date dateTime = gmt.parse(dateStr); String dateString = cst.format(dateTime); System.out.println(dateString); }

输出结果:

2022-10-26 22:22:22

CST格式的日期转换为GMT时间

代码:

public static void main(String[] args) throws ParseException { Date date = new Date(); DateFormat gmt = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH); gmt.setTimeZone(TimeZone.getTimeZone("GMT")); String dateStr = gmt.format(date); System.out.println(dateStr); }

输出结果:

Fri, 23-Sep-2022 03:05:42 GMT

GMT字符串转化为本地时间

public static void main(String[] args) throws ParseException { DateFormat format = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH); format.setTimeZone(TimeZone.getTimeZone("GMT")); Date parse = format.parse("Fri, 23-Sep-2022 03:15:55 GMT"); System.out.println(parse); }

输出结果:

Fri Sep 23 11:15:55 CST 2022

相关阅读:

https://ssxtx.blog.csdn.net/article/details/108369391

点赞 收藏 关注

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
python中重写父类方法直接写方法名(python中重写与调用方法是什么) php如何配置环境(phpstorm配置php环境)