Reputation: 193
I want to set the text inside a date picker to the result of an SQL query. I do the following to get the date;
deadlineDatePrompt = rs.getString("Deadline");
This obviously stored the date as a String. I try to set the value of my DatePicker using;
deadlineDatePicker.setText(deadlineDatePrompt);
This however does not work as setText isn't defined for DatePicker. Changing setText()
to setValue()
does not work either as deadlineDatePrompt
is stored as a string. Changing it to a LocalDate
does not fix the issue as then rs.getString
does not work.
Is there a work around for this so that I can get the result of the date set inside my database and display it inside the DatePicker?
Upvotes: 0
Views: 3495
Reputation: 209340
You can retrieve the value from the database as a java.sql.Date
. A DatePicker
requires a java.time.LocalDate
, so you need to convert it: this is pretty easy as java.sql.Date
defines a toLocalDate()
method.
So
java.sql.Date deadlineDatePrompt = rs.getDate("Deadline");
deadlineDatePicker.setValue(deadlineDatePrompt.toLocalDate());
Upvotes: 2
Reputation: 1632
You should read 'Deadline' field as a date using getDate:
rs.getDate("Deadline");
Be aware that get Date return java.sql.Date not java.util.Date so you need to convert one to another.
Upvotes: 0