Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set javafx datepicker value correctly?

Tags:

java

javafx

I used this method to setting DatePicker value:

public static final LocalDate LOCAL_DATE (String dateString){
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    LocalDate localDate = LocalDate.parse(dateString, formatter);
    return localDate;
}

try {
    datePicker.setValue(LOCAL_DATE("2016-05-01");
} catch (NullPointerException e) {}

But sometimes throw an exception:

java.time.format.DateTimeParseException: Text '' could not be parsed at index 0

So what's the wrong here?

like image 565
Hussein Ahmed Avatar asked May 01 '16 15:05

Hussein Ahmed


1 Answers

You specify a format for parsing the date of dd-MM-yyyy:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

but then you provide the date in a format that doesn't match that:

datePicker.setValue(LOCAL_DATE("2016-05-01"));

Obviously, "2016-05-01" is not in the format "dd-MM-yyyy".

Try

datePicker.setValue(LOCAL_DATE("01-05-2016"));
like image 194
James_D Avatar answered Sep 25 '22 17:09

James_D