繁体   English   中英

使用 java.time 将 UTC 日期时间转换为本地日期时间

[英]Convert UTC datetime to local datetime using java.time

我有一个像这样的 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());

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

任何帮助将不胜感激。

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

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

Output

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

可以有很多方法来实现结果。 一个简单的方法是

  1. 将给定的字符串解析为LocalDateTime
  2. 使用LocalDateTime#atOffset OffsetDateTime
  3. 使用OffsetDateTime#atZoneSameInstant将生成的OffsetDateTime转换为ZonedDateTime ZoneId.of("America/Tijuana")处的 ZonedDateTime。
  4. 使用ZonedDateTime#toLocalDateTime从生成的ZonedDateTime中获取LocalDateTime
  5. 如果需要,将此LocalDateTime格式化为所需的字符串。
LocalDateTime
    .parse("2022-11-22T17:15:00") // Parse the given date-time string into LocalDateTime
    .atOffset(ZoneOffset.UTC) // Convert it into a ZonedDateTime at UTC
    .atZoneSameInstant(ZoneId.of("America/Tijuana")) // Convert the result into a ZonedDateTime at another time-zome
    .toLocalDateTime() // Get the LocalDateTime out of the ZonedDateTime
    .format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", Locale.ENGLISH))); // If required

演示

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")
                .atOffset(ZoneOffset.UTC)
                .atZoneSameInstant(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);
    }
}

Output :

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

请注意,如果它们为零, LocalDateTime#toString会删除秒和秒的分数。 假设您想保留它们(正如您在问题中发布的那样),您可以使用DateTimeFormatter ,如上所示。

另一种方法:

或者,您可以在 ISO 8601 格式的日期时间字符串的末尾使用 append Z以启用Instant来解析它,然后使用Instant#atZoneInstant转换为与ZonedDateTime ZoneId.of("America/Tijuana")对应的 ZonedDateTime . 符号Z指的是日期时间字符串中的 UTC。

rest的步骤还是一样的。

演示

public class Main {
    public static void main(String[] args) {
        String text = "2022-11-22T17:15:00";
        text = text + "Z"; // Z refers to UTC
        Instant instant = Instant.parse(text);
        LocalDateTime ldt = instant.atZone(ZoneId.of("America/Tijuana")).toLocalDateTime();
        System.out.println(ldt);
    }
}

Output :

2022-11-22T09:15

Trail:Date Time了解有关现代日期时间 API 的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM