【问题标题】:Java How to convert specific time to different time zoneJava如何将特定时间转换为不同的时区
【发布时间】:2021-05-27 19:03:12
【问题描述】:

我有一个时间,例如25 May 2021 02:00:00 PM EDT(现在不是当地时间),我需要将该时间转换为不同时区的时间(例如:巴黎时间),应该是25 May 2021 08:00:00 PM CET 如何使用 java 做到这一点?

【问题讨论】:

  • 这能回答你的问题吗? Timezone conversion
  • 0。不要使用DateCalendarSimpleDateFormat。 1. 解析为ZonedDateTime。 2. 用atZone之类的东西指定新时区。
  • 为什么不直接从互联网上查询时间 API 而不是重新发明轮子?
  • 问题是当我使用 ZonedDateTime zonedDateTime = sentTime.atZone(ZoneId.of("Europe/Paris"));它只是简单地给了我这个: 2021-05-27T15:17:50.805+02:00[Europe/Paris] ,这是我当前的时间,没有转换为“欧洲/巴黎”
  • @reborn 我很乐意,但这就是我要问的?

标签: java date datetime


【解决方案1】:

避免使用EDTCET,因为它们不是实时时区。实时区域的名称格式为Continent/Region。例如,America/New_YorkEurope/Paris

ZonedDateTime zdt = 
    ZonedDateTime.of (
        2021 , 
        5 , 
        25 ,
        2 ,
        0 ,
        0 , 
        0 ,
        ZoneId.of( "America/Montreal" ) 
    )
;

调整到另一个区域。

ZoneId zoneTunis = ZoneId.of( "Africa/Tunis" ) ; 
ZonedDateTime zdtTunis = zdt.withZoneSameInstant( zoneTunis ) ;

搜索以了解更多信息。这些话题已经在 Stack Overflow 上讨论过很多次了。

【讨论】:

  • 这看起来像我正在寻找的东西,我缺少使用 withZoneSameInstant ,这就是它不起作用的原因。还有任何建议如何将 EDT 转换为“America/New_York”?因为这就是我的输入
  • @harmatii1 您会在 Stack Overflow 上找到许多关于解析此类字符串的现有问题。 java.time 类可以对这些伪时区进行猜测,但不能将它们完美地映射到实时时区,因为有些不是唯一的:CST、IST 等。教育您的数据发布者有关日期时间值的正确数据交换:(a) 使用 UTC(零偏移)和 (b) 仅使用 ISO 8601 格式。
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 2018-11-28
  • 2015-08-30
  • 2014-03-23
  • 2021-12-18
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多