【问题标题】:Convert datetime string in one timezone to another using offset使用偏移量将一个时区中的日期时间字符串转换为另一个时区
【发布时间】:2018-01-15 07:27:33
【问题描述】:

我有一个位于 EST 时区的日期时间字符串“2018-01-15 01:16:00”。我想使用 UTC 偏移量将其动态转换为另一个时区。我的 javascript 代码将此 UTC 偏移量作为参数传递,并且 servlet 必须将此日期时间字符串转换/格式化为由提供的偏移量标识的时区。

我尝试了许多方法,包括 oracle tutorials 中记录的方法,但无法找到解决方案。

以下是我正在尝试的代码,非常感谢任何帮助。

private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DEFAULT_TIME_ZONE = ZoneId.SHORT_IDS.get("EST");

public static void main(String[] args) throws Exception {
    String dateTime = "2018-01-15 02:35:00";
    //parse the datetime using LocalDateTime
    LocalDateTime defaultDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATE_FORMAT));

    //get the datetime in default timezone
    ZoneId defaultZoneId = ZoneId.of(DEFAULT_TIME_ZONE);
    ZonedDateTime defaultZoneDateTime = defaultDateTime.atZone(defaultZoneId);
    System.out.println("EST time: "+defaultZoneDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
    ZonedDateTime utcZonedDateTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));
    String utcTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    System.out.println("UTC : "+utcTime);

    //IST timezone
    ZoneOffset offset = ZoneOffset.of("+05:30");
    OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);
    String targetTimeZone = offsetDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    System.out.printf("target time : "+targetTimeZone);

}

输出

EST time: 2018-01-15 02:35:00
UTC : 2018-01-15 07:37:00
target time : 2018-01-15 07:37:00

预计目标时间:2018-01-15 13:05:00

【问题讨论】:

  • 首先要检查 - 您真正所说的“EST”是什么意思?您是指“东部时间”(UTC+5 或 UTC+6,具体取决于一年中的时间)还是真正的“东部标准时间”(全年为 UTC+5)? 总是最好使用完整的时区 ID,例如美国/纽约。
  • 下一步:提供带有实际输出的完整示例非常棒,但如果您也提供 预期 输出会更有帮助。
  • 谢谢,我的意思是真正的“东部标准时间”,因为这个日期时间字符串来自第 3 方 API,并且没有提供准确的信息。
  • 谢谢@JonSkeet 我已经添加了预期的输出。我发布的代码是一个带有硬编码输入日期时间字符串的完整示例。
  • (我之前的 cmets 应该提到 UTC-5 和 UTC-4 而不是 UTC+5 和 UTC+6 顺便说一句。)

标签: java datetime java-8 timezone-offset


【解决方案1】:

当前的问题是这一行:

OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);

也就是说,您想要相同的 local 日期/时间,但具有指定的偏移量。这会改变所代表的时间点。

相反,您确实希望在时间上表示相同的即时,但在特定的偏移量处。所以最短的解决办法是:

OffsetDateTime offsetDate = utcZonedDateTime.toInstant().atOffset(offset);

但是,还有许多其他方面可以改变:

  • 首选ZoneOffset.UTC 而不是ZoneId.of("UTC")
  • 使用EST 作为时区令人困惑 - 不清楚您是否希望它表示“东部时间”(在 EST 和 EDT 之间转换)或 UTC-5 的纯标准时间。假设您实际上是指“东部时间”,最好使用 America/New_York 作为区域 ID。
  • 如果输入字符串表示东部时间的跳过或模棱两可的值,则不清楚您希望发生什么。这些发生在 DST 转换前后。

接下来,您根本不需要将东部时间的 ZonedDateTime 转换为 UTC 的 ZonedDateTime。直接将其转换为瞬间:

OffsetDateTime target = defaultZoneDateTime.toInstant().at(offset);

或者为目标创建一个ZonedDateTime

ZonedDateTime target = defaultZoneDateTime.withZoneSameInstant(offset);

鉴于偏移量不是真正时区,我可能会选择其中的第一个。

【讨论】:

    【解决方案2】:

    你正在使用

    OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset)
    

    创建你的目标。因此,您在目标偏移量中构造了一个 OffsetDateTime,其 LocalDateTime 等于 UTC 区域中的 LocalDateTime。

    您想要的是与您用于从 EST 时间到 UTC 的转换完全相同的转换:保持相同的时刻,但转到不同的时区:

    defaultZoneDateTime.withZoneSameInstant(offset);
    

    或者,如果您真的想要 OffsetDateTime 而不是 ZonedDateTime:

    OffsetDateTime.ofInstant(defaultZoneDateTime.toInstant(), offset);
    

    【讨论】:

    • 这可能只是我的口味,但要获得OffsetDateTime,我会选择defaultZoneDateTime.withZoneSameInstant(offset).toOffsetDateTime()
    猜你喜欢
    • 2020-01-08
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2015-02-21
    相关资源
    最近更新 更多