Javaで「日付時刻解析例外」を修正する方法

Chornologic:

私はフォーマットに入力された日付をフォーマットしようとしています日付時刻フォーマッタは、(D / MM / YYYY)を以下に示しています

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");

私は、文字列として誕生日のユーザー入力を取るために、このフォーマッタを使用して、一時は出産のユーザー入力された日付を格納してLOCALDATE変数などの店舗に至るまでそれを解析しようとしています

public void addCustomer() throws ParseException {
        customerID++;
        //Create Scanner
        Scanner scan = new Scanner(System.in);

        //Take user input
        System.out.println("Please enter your name: ");
        String name = scan.nextLine();
        System.out.println("Please enter your Date of Birth(dd/MM/yyyy): ");
        String temp = scan.nextLine();
        LocalDate date = LocalDate.parse(temp);
        Customer c = new Customer(customerID, name, date, false, "N/A");
        customers.add(c);
    }

しかし、これは常にDateTimeParseExceptionを返します。テキストは解析できませんでした。私はいつもこの例外が発生する日付時刻フォーマッタを設定しています方法に問題ですか?下に示された

Exception in thread "main" java.time.format.DateTimeParseException: Text '27/01/1999' could not be parsed at index 0
    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.LocalDate.parse(LocalDate.java:428)
    at java.base/java.time.LocalDate.parse(LocalDate.java:413)
    at BikeNow.addCustomer(BikeNow.java:153)
    at BikeNow.main(BikeNow.java:98)
Ros5292:

あなたのパスDateTimeFormatterオブジェクトを。

これを変える:

LocalDate date = LocalDate.parse(temp);

...これに:

LocalDate date = LocalDate.parse(temp, format);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=120000&siteId=1