0
Hi, I am trying to pass a date set by the user to the database, but whenever I click add the application crasha and I have the following error:
java.lang.Illegalargumentexception: Cannot format Given Object as a Date.
The code to add the data to the BD is:
public void add(String date){
SQLiteDatabase db = this.getWritableDatabase();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
ContentValues cv = new ContentValues();
cv.put(COLUMN_DATE, dateFormat.format(date));
And this is the code I have in Activity, to call the datepickerdialog and pass the entered data to the method that adds the information to db.
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar dataSelect = Calendar.getInstance();
dataSelect.set(year,month,dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
adddate.setText(format.format(dataSelect.getTime()));
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
adddate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerDialog.show();
}
});
btnsubtmit = findViewById(R.id.btnadd);
btnsubtmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataBase myDB = new DataBase(AddTarefas.this);
myDB.add(adddate.getText().toString().trim());
}
});