Reputation: 323
I can not understand what the problem, have a look at my code.
Constructor of Note, accepts an object type Date:
public Nota(String titolo, String testo, Date data, int colore) {
this.titolo = titolo;
this.testo = testo;
this.data = data;
// controlla che l'id del radiobutton non sia -1
if( colore == -1 ){
Log.w("Color", "Errore id Colore del Radio Button");
}else{
this.colore = colore;
}
}
The Date is passed here, when occurs saving the Note in the DB:
private void insertInDB() {
DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALY);
Calendar now = Calendar.getInstance(Locale.ITALY);
Date date = now.getTime();
String dteStr = rifDate.getText().toString();
// provo a parsare una String in Date
try{
date = format.parse(rifDate.getText().toString());
// cattura l'eccezione
}catch (ParseException pe){
pe.printStackTrace();
}
nota = new Nota(rifTitleNote.getText().toString(), rifWriteEdit.getText().toString(), date, idRadioGroup);
now.setTime(date);
// if the insertion is successful ok, otherwise return -1
boolean insNoteCheck = dbNote.insertNote(nota);
}
Here, is the method of insert in the class Database which extends SQLiteOnHelper:
public boolean insertNote(Nota nota){
// boolean inizializzat a false utilizzata come return
boolean resultInsert = false;
// repository dei dati in modalità scrittura
SQLiteDatabase dbLite = this.getWritableDatabase();
// utilizza un ContentValues come mappa di valori, dove le columns rappresentano le chiavi
ContentValues values = new ContentValues();
values.put("titoloNota", nota.getTitolo());
values.put("testoNota", nota.getTesto());
values.put("dataNota", nota.getData()); // getData() return a String
values.put("coloreNota", nota.getColore());
Method getData() which return a String:
public String getData() {
SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yyyy", Locale.ITALY);
return DateFormat.getDateInstance().format(data); // data is Data type passed to the Constructor
}
Then, I have an Inner static class for the DatePickerDialog, where I set the EditText of date, here the method onDateSet():
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
EditText date = (EditText) getActivity().findViewById(R.id.editData);
//System.out.println("QUESTA DATAA :"+ anno + " " + mese + " " + giorno);
// setto il testo della Data aggiungendo +1 al mese poichè in Calendar sono rappresentati(0-11)
date.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
and finally I have an Adapter for the list of element, in the method onBindViewHolder() that there is this line to display the date in the list
holder.txtDate.setText("Data: " + arrayList.get(position).getDataa());
I have done several times to understand what the problem, but unfortunately no way. Through the System.out.println() when creating the Note in the Logcat I see the date correctly. But I have the impression that the date will not be saved in DB, in fact in the list (Adapter), the dates are all initialized 1/gen/1970.
Upvotes: 0
Views: 70
Reputation: 850
As per this link Datatypes in SQLLite
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
So one way to store dates is to use INTEGER as the column type. Then use date.getTime() to get the epoch time, i.e. epoch time as a long type in milliseconds, as the value to store.
When reading from the database, use date.setTime(long) to set the date variable.
Upvotes: 1