将字符串转换为 LocalDateTime 的问题

问题描述

当我运行这个时,我得到了异常: 线程“main”中的异常 java.time.format.DateTimeParseException:无法解析文本“2020-12-15 13:48:52”:ClockHourOfAmPm 的值无效(有效值

我写信给控制台:2020-12-15 13:48:52

public class Main {
    public static void main(String[] args) {


        Scanner scanner = new Scanner(system.in);
        System.out.println("Podaj datę:");
        String input = scanner.nextLine();


        if (input.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
            DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
            LocalDateTime localDateTime = LocalDateTime.parse(input,dateTimeFormatter1);
            printDateTime(localDateTime);
        } else if (input.matches("\\d{2}.\\d{2}.\\d{4} \\d{2}:\\d{2}:\\d{2}")) {
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm:ss");
            LocalDateTime localDateTime2 = LocalDateTime.parse(input,dateTimeFormatter2);
            printDateTime(localDateTime2);
        } else if (input.matches("\\d{4}-\\d{2}-\\d{2}")) {
            DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime localDateTime3 = LocalDateTime.parse(input,dateTimeFormatter3);
            printDateTime(localDateTime3);
        } else {
            System.out.println("Zły format");
        }
    }

    private static void printDateTime(LocalDateTime localDateTime) {
        System.out.println("Czas lokalny: " + zoneddatetime.Now());
        System.out.println("UTC: " + zoneddatetime.of(localDateTime,ZoneId.of("UTC")));
        System.out.println("Londyn: " + zoneddatetime.of(localDateTime,ZoneId.of("London")));
        System.out.println("Los Angeles: " + zoneddatetime.of(localDateTime,ZoneId.of("Los Angeles")));
        System.out.println("Sydney: " + zoneddatetime.of(localDateTime,ZoneId.of("Sydney")));
    }
}

解决方法

对于此要求,请勿使用正则表达式,因为它会使您的程序不必要地复杂且容易出错。您可以将方括号中的可选模式与 DateTimeFormatter 一起使用。除此之外,

  1. 使用 DateTimeFormatterBuilder#parseDefaulting 将字段的默认值(例如 HOUR_OF_DAY)附加到格式化程序以用于解析。
  2. 使用 ZonedDateTime#withZoneSameInstant 返回具有不同时区的此日期时间的副本,保留瞬间。
  3. 使用时区的标准命名约定(地区/城市),例如Europe/London
  4. HH 使用 hh 而不是 HOUR_OF_DAY(即 24 小时格式的时间)。符号 hh 与指定 a(即 12 小时格式的时间)的 am/pm 一起使用。

演示:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .appendPattern("[uuuu-MM-dd HH:mm:ss][dd.MM.uuuu HH:mm:ss][uuuu-MM-dd]")
                .parseDefaulting(ChronoField.HOUR_OF_DAY,0)
                .parseDefaulting(ChronoField.MINUTE_OF_HOUR,0)
                .parseDefaulting(ChronoField.SECOND_OF_MINUTE,0)
                .toFormatter(Locale.ENGLISH);

        System.out.print("Podaj datę:");
        String input = scanner.nextLine();
        LocalDateTime localDateTime = LocalDateTime.parse(input,dtf);
        System.out.println(localDateTime);

        printDateTime(LocalDateTime.parse(input,dtf));
    }

    private static void printDateTime(LocalDateTime localDateTime) {
        // Default timezone
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdtDefaultTimeZone = localDateTime.atZone(zoneId);

        System.out
                .println("Date-time at " + zoneId + ": " + zdtDefaultTimeZone);
        System.out.println("At UTC: " + zdtDefaultTimeZone.withZoneSameInstant(ZoneId.of("Etc/UTC")));
        System.out.println("In London: " + zdtDefaultTimeZone.withZoneSameInstant(ZoneId.of("Europe/London")));
        System.out
                .println("In Los Angeles: " + zdtDefaultTimeZone.withZoneSameInstant(ZoneId.of("America/Los_Angeles")));
        System.out.println("In Sydney: " + zdtDefaultTimeZone.withZoneSameInstant(ZoneId.of("Australia/Sydney")));
    }
}

示例运行:

Podaj datę:2020-02-23 10:15:20
2020-02-23T10:15:20
Date-time at Europe/London: 2020-02-23T10:15:20Z[Europe/London]
At UTC: 2020-02-23T10:15:20Z[Etc/UTC]
In London: 2020-02-23T10:15:20Z[Europe/London]
In Los Angeles: 2020-02-23T02:15:20-08:00[America/Los_Angeles]
In Sydney: 2020-02-23T21:15:20+11:00[Australia/Sydney]

另一个示例运行:

Podaj datę:23.02.2020 10:15:20
2020-02-23T10:15:20
Date-time at Europe/London: 2020-02-23T10:15:20Z[Europe/London]
At UTC: 2020-02-23T10:15:20Z[Etc/UTC]
In London: 2020-02-23T10:15:20Z[Europe/London]
In Los Angeles: 2020-02-23T02:15:20-08:00[America/Los_Angeles]
In Sydney: 2020-02-23T21:15:20+11:00[Australia/Sydney]

另一个示例运行:

Podaj datę:2020-02-23
2020-02-23T00:00
Date-time at Europe/London: 2020-02-23T00:00Z[Europe/London]
At UTC: 2020-02-23T00:00Z[Etc/UTC]
In London: 2020-02-23T00:00Z[Europe/London]
In Los Angeles: 2020-02-22T16:00-08:00[America/Los_Angeles]
In Sydney: 2020-02-23T11:00+11:00[Australia/Sydney]

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

,

您在 DateTimeFormatter 中的 DateTime 模式会导致您在此处看到的问题。您需要使用 HH 而不是 hh

  • hh:这是一种使用 12 小时制的小时模式,带有 AM/PM 指示。
  • HH:这是一种使用 24 小时制的小时模式。接受 0-23 之间的输入

旁注,您使用的区域似乎无效。 相应的区域将是

Europe/London
America/Los_Angeles
Australia/Sydney

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...