将字符串解析为特定格式的日期

Parse string to date of specific format

我有一个格式如下 String stringDate = "2019-04-25T07:03:17.428Z"; 的字符串,我需要将其转换为格式为 2019-04-25 07:03:17

的 LocalDateTime 对象

我试过 LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) 但我得到这个解析异常:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-04-25T07:03:17.428Z' could not be parsed at index 10
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at com.company.Main.main(Main.java:12)

转换成特定格式的方法是什么?

从根本上说,您的输入不是 LDT。这是一个 ZDT——Z 是一个区域 (ZonedDateTime)。因此,无法将此字符串 直接 转换为 LDT,这很好,因为那没有任何意义。

将其转换为 ZDT。然后将 that 转换为 LDT。这是一个单一的方法调用。

你想要的(但不想要的)

    String stringDate = "2019-04-25T07:03:17.428Z";
    LocalDateTime ldt = LocalDateTime.parse(stringDate, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
            .truncatedTo(ChronoUnit.SECONDS);
    System.out.println(ldt);

输出是

2019-04-25T07:03:17

它给了你你说的你想要的,但是作为rzwitserloot points out in the other ,它没有意义。它会忽略字符串中的偏移量 Z。偏移量对于将日期和时间解释为时间点至关重要。如果字符串是 2019-04-25T07:03:17.428+13:00,即使字符串表示早于 13 小时的时间点,您仍然会得到相同的输出。

truncatedTo() 的调用去掉了秒的小数部分。如果你能忍受那一小部分秒,就不要打那个电话了。

您可能想要什么

为了更明智的转换:

    ZoneId zone = ZoneId.of("Europe/Zagreb");
    
    Instant pointInTime = Instant.parse(stringDate);
    LocalDateTime ldt = pointInTime.atZone(zone)
            .toLocalDateTime()
            .truncatedTo(ChronoUnit.SECONDS);

    System.out.format("Date and time in %s: %s%n", zone, ldt);

Date and time in Europe/Zagreb: 2019-04-25T09:03:17

现在 07:03 UTC 时间已转换为 09:03 中欧夏令时。

编辑:如果您确实需要 UTC 中的 LocalDateTime,例如对于需要此的数据库列,您可以通过以下方式在您的代码中明确说明这一事实:

    LocalDateTime ldt = pointInTime.atOffset(ZoneOffset.UTC)
            .toLocalDateTime()
            .truncatedTo(ChronoUnit.SECONDS);

    System.out.format("Date and time in UTC: %s%n", ldt);

Date and time in UTC: 2019-04-25T07:03:17

你的代码出了什么问题?

要了解您的失败原因,让我们看看您收到的异常消息:

Text '2019-04-25T07:03:17.428Z' could not be parsed at index 10

索引 10 是表示时间部分开始的 T 所在的位置。让我们与您的格式模式字符串进行比较:yyyy-MM-dd HH:mm:ss。这在时间部分之前有一个 space 而不是 T。这就是例外的原因。如果您需要在模式中指定 T 必须存在,我们需要将其用单引号引起来,例如 yyyy-MM-dd'T'HH:mm:ss.

可以先用java.time.Instant解析,再用解析ofInstant of java.time.LocalDateTime 示例:

String stringDate = "2019-04-25T07:03:17.428Z";
var instant = Instant.parse(stringDate);
var localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);