【问题标题】:Convert UTC datetime to local datetime using java.time使用 java.time 将 UTC 日期时间转换为本地日期时间
【发布时间】:2022-11-14 18:31:57
【问题描述】:

我有一个这样的 UTC 日期时间(字符串):2022-11-22T17:15:00

像这样的 ZoneID:"America/Tijuana"

使用 java.time API,我想获取该区域的实际日期时间,即:2022-11-22T09:15:00(时间是 09:15 而不是 17:15)

  • ZonedDateTime.toLocalDateTime() 返回:2022-11-22T17:15
  • ZonedDateTime.toString() 返回: 2022-11-22T17:15-08:00[America/Tijuana]

以上都没有给我我正在寻找的东西。

这是我的代码:

    ZoneId zonaID = ZoneId.of('America/Tijuana');
    CharSequence dateUTC = "2022-11-22T17:15:00";
    LocalDateTime dateTimeL = LocalDateTime.parse(dateUTC);
    ZonedDateTime myZDT = ZonedDateTime.now();
    ZonedDateTime myZDTFinal = myZDT.of(dateTimeL, zonaID);
    System.out.println("using toLocalDateTime: " + myZDTFinal.toLocalDateTime());
    System.out.println("using toString: " + myZDTFinal.toString());

我知道这可能是一个重复的问题,但是关于日期时间的问题太多了,我只是无法弄清楚这一点。

任何帮助将不胜感激。

【问题讨论】:

  • myZDT.of(dateTimeL, zonaID) 正在调用一个静态方法,就好像它是一个实例方法一样。我会强烈避免这样做——这真的很令人困惑。
  • 你已经得到的答案是正确的答案。您的主要问题是您假设字符串“2022-11-22T17:15:00”是 UTC,但在字符串本身中没有这样的指示。换句话说,它是一个 LocalDateTime。如果您知道它是 UTC,您首先需要从您拥有的 LocalDateTime 和 UTC ZoneId 创建 ZonedDateTime,然后将其转换为您的 zoneID“America/Tijuana”

标签: java datetime java-8 localdatetime


【解决方案1】:

您必须将日期转换为 UTC,然后使用 withZoneSameInstant 将此区域转换为您的预期区域,如下所示:

ZonedDateTime toUTCZone  = ZonedDateTime.of(dateTimeL, ZoneOffset.UTC);
ZonedDateTime myZDTFinal = toUTCZone.withZoneSameInstant(zonaID);

输出

2022-11-22T09:15-08:00[America/Tijuana]

【讨论】:

  • 一个小的更新:ZoneOffset.UTC 是指 UTC ZoneId 的更惯用方式。
  • 谢谢@rzwitserloot 我同意
【解决方案2】:

ZonedDateTime#withZoneSameInstant

使用它将ZonedDateTime 转换为对应ZoneId 的另一个ZonedDateTime

最后,使用ZonedDateTime#toLocalDateTimeLocalDateTimeZonedDateTime 中取出。

LocalDateTime
    .parse("2022-11-22T17:15:00") // Parse the given date-time string into LocalDateTime
    .atZone(ZoneOffset.UTC) // Convert it into a ZonedDateTime 
    .withZoneSameInstant(ZoneId.of("America/Tijuana")) // Convert the result into a ZonedDateTime at another time-zome
    .toLocalDateTime() // Convert the result into LocalDateTime

演示

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDateTime ldtInTijuana = LocalDateTime.parse("2022-11-22T17:15:00")
                .atZone(ZoneOffset.UTC)
                .withZoneSameInstant(ZoneId.of("America/Tijuana"))
                .toLocalDateTime();
        System.out.println(ldtInTijuana);

        // Custom format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
        String formatted = ldtInTijuana.format(formatter);
        System.out.println(formatted);
    }
}

输出

2022-11-22T09:15
2022-11-22T09:15:00

请注意,LocalDateTime#toString 会删除秒和秒值(如果它们为零)。如果你想保留它们(正如您在问题中发布的那样),您可以使用DateTimeFormatter 来执行此操作,如上所示。

从以下位置了解有关现代日期时间 API 的更多信息Trail: Date Time.

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2019-06-21
    • 2011-06-13
    • 2018-07-15
    相关资源
    最近更新 更多