使用偏移量将一个时区中的日期时间字符串转换为另一个时区

Convert datetime string in one timezone to another using offset

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

我尝试了很多方法,包括 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

您正在使用

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

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

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

defaultZoneDateTime.withZoneSameInstant(offset);

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

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

眼前的问题是这一行:

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

这就是说您想要相同的 local date/time,但具有指定的偏移量。这改变了表示哪个瞬间。

相反,您确实希望在时间上表示相同的 瞬间 ,但在特定的偏移量处。所以最短的修复是:

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

但是,还有许多其他方面可以进行更改:

  • ZoneId.of("UTC")更喜欢ZoneOffset.UTC
  • 使用 EST 作为时区令人困惑 - 不清楚您希望它表示 "Eastern Time"(在 EST 和 EDT 之间变化)还是 UTC-5 的纯标准时间。假设您实际上是指 "Eastern Time",最好使用 America/New_York 作为区域 ID。
  • 不清楚如果输入字符串代表东部时间的跳过值或不明确值,您希望发生什么。这些发生在 DST 转换前后。

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

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

或者为目标创建一个 ZonedDateTime

ZonedDateTime target = defaultZoneDateTime.withZoneSameInstant(offset);

考虑到偏移量 不是真正的 时区,我可能会选择第一个。