将 String 转换为 LocalDateTime 的问题

Problem with convert String to 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 中的 DateTime 模式导致您在此处看到的问题。您需要使用 HH 而不是 hh

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

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

Europe/London
America/Los_Angeles
Australia/Sydney

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

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

演示:

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