Java 使用LocalDate获取输入

Java 使用LocalDate获取输入,java,localdate,dateformatter,Java,Localdate,Dateformatter,各位!!我正试图想出一种方法,使用LocalDate从用户那里获取日期输入。我收到一个错误,上面写着“类型不匹配:无法从字符串转换为LocalDate”。我知道为什么会发生这个错误,但我想知道是否有其他方法可以解决这个问题 String newName = stringInput("Enter a product name: "); String newStore = stringInput("Enter a store name: "); LocalDate newDate = dateInp

各位!!我正试图想出一种方法,使用LocalDate从用户那里获取日期输入。我收到一个错误,上面写着“类型不匹配:无法从字符串转换为LocalDate”。我知道为什么会发生这个错误,但我想知道是否有其他方法可以解决这个问题

String newName = stringInput("Enter a product name: ");
String newStore = stringInput("Enter a store name: ");
LocalDate newDate = dateInput("Enter a date (like 3/3/17): ");
double newCost = doubleInput("Enter cost: ");

    /* the third parameter of Purchase2 is a LocalDate which I think is the reason for my error.
     * Is there any way to get around this?
     */
Purchase2 purchase = new Purchase2(newName, newStore, newDate, newCost);
            purchases.add(purchase); // I'm adding these to an ArrayList


    /*
     * This is the method I created for newDate
     * I need to take the date as String and convert it to LocalDate
     */
 public static String dateInput(String userInput) {

    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("M/d/yyyy");
    LocalDate date = LocalDate.parse(userInput, dateFormat);


    System.out.println(date);
    return userInput;
}

我对Java真的很陌生,所以任何帮助都将不胜感激!谢谢大家!

dateInput
的返回值更改为
LocalDate

public static LocalDate dateInput(String userInput) {

    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("M/d/yyyy");
    LocalDate date = LocalDate.parse(userInput, dateFormat);


    System.out.println(date);
    return date ;
}
并修改:

LocalDate newDate = dateInput(stringInput("Enter a date (like 3/3/17): "));

除此之外,您还需要关心
yyyy
formatter

只需将返回类型更改为
LocalDate
返回日期。您的意思是将我的dateInput参数从字符串更改为LocalDate?感谢您的快速回复!