How to set javafx datepicker value correctly?

I used this method to set the datepicker value, but occasionally threw an exception:

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) { } //the exception: java.time.format.DateTimeParseException: Text '' could not be parsed at index 0 

So what is wrong here?

+5
source share
1 answer

You define the format for parsing the dd-MM-yyyy date:

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

but then you specify the date in a format that does not match this:

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

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

Try

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

All Articles