Java处理时间格式CST和GMT转换

本文介绍了CST和GMT在编程中的含义,CST可能代表多个时区,而GMT指格林尼治标准时间。文章通过示例代码展示了如何在Java中将CST格式转换为yyyy-MM-ddHH:mm:ss格式,以及将CST日期转换为GMT时间和将GMT字符串转化为本地时间的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在编程中处理日期格式时,通常会遇到带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

在这里插入图片描述

点赞 收藏 关注

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三省同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值