Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复';日期时间分析异常';在爪哇_Java_Datetime Format_Localdate_Parseexception - Fatal编程技术网

Java 如何修复';日期时间分析异常';在爪哇

Java 如何修复';日期时间分析异常';在爪哇,java,datetime-format,localdate,parseexception,Java,Datetime Format,Localdate,Parseexception,我有一个日期时间格式化程序,我正在尝试将输入的日期格式化为如下所示的格式(d/MM/yyyy) DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy"); Exception in thread "main" java.time.format.DateTimeParseException: Text '27/01/1999' could not be parsed at index 0 at java.ba

我有一个日期时间格式化程序,我正在尝试将输入的日期格式化为如下所示的格式(d/MM/yyyy)

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
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)
然后,我使用这个格式化程序将用户输入的出生日期作为字符串,然后尝试解析它,将其存储为LocalDate变量,temp存储用户输入的出生日期

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:无法解析文本。我如何设置日期时间格式化程序以始终导致此异常的问题?如下所示

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
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)

我认为您忘记了参数,下面是修复方法:

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();

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date = LocalDate.parse(temp, formatter);
        Customer c = new Customer(customerID, name, date, false, "N/A");
        customers.add(c);
}

传递您的
DateTimeFormatter
对象

更改此项:

LocalDate date = LocalDate.parse(temp);
……为此:

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

您的输入是什么?d/MM/yyyy?除了1-9天之外的其他日期呢?这在我的环境中起作用。。。请向我们显示导致
异常的堆栈跟踪和输入。您的代码实际上没有使用您创建的
format
变量。
LocalDate.parse(temp,format)