I am new to JFrame
. I just started learning about creating a Java project. I created an input field as a date and inserted a DateChooseCombo
. I have 2 problems.
"Cannot format given Object as a Date"
The code for the date is as follows:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String addDate = dateFormat.format(txt_AddDate.getSelectedDate());
ps.setString(3 ,addDate);
The date format used is 04/30/2021
.
Can anyone help me to solve these two problems?
Method getSelectedDate()
, in class datechooser.beans.DateChooserCombo
, returns a java.util.Calendar.
Method format, in class java.text.DateFormat
(which is superclass of java.text.SimpleDateFormat
and hence inherited by SimpleDateformat
) requires a parameter of type java.util.Date. A Calendar
is not a Date
and that's why you are getting the error. Java cannot convert a Calendar
to a Date
.
However, class Calendar
has method getTime which returns a Date
.
So you need to change the second line of the code, that you posted in your question, to the following.
String addDate = dateFormat.format(txt_AddDate.getSelectedDate().getTime());
You can also refer to the following question (and answer) :
Can't get date from DateChooserCombo