David
David

Reputation: 221

Insert datetime value in android sqlite

I'm having a problem inserting some datetime values in my sqlite database.

I have two datepickers, i can choose a date, but after that, when I insert it into my database, I don't know why but the row for the 2 dates have the current date.. How can I do to insert the date I selected in the datepicker ?

In my database, I declared those columns as DATETIME.

Here's my get-setter class for the dates:

    public String getDate_debut() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd-MM-yyyy HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);
}

public void setDate_debut(String date_debut) {
    this.date_debut = date_debut;
}

public String getDate_fin() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd-MM-yyyy HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);
}

public void setDate_fin(String date_fin) {
    this.date_fin = date_fin;
}

Here's how I get the date of one datepicker. I'm not sure about the way I format my string, and if I need to format or if I can just add as a string.

 private DatePickerDialog.OnDateSetListener datepickerdernier
        = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

        year_x2 = year;

        //les DatePicker
        month_x2 = month +1;
        day_x2 = dayOfMonth;
        datefin = (TextView)findViewById(R.id.TVDatePickerDernier);
        datefin.setText(year_x2+"-"+month_x2+"-"+day_x2);
    }
};


            String date2 = datefin.getText().toString();
            //im not sure about the following lines
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Date date_dernier = dateFormat.parse(date2);

And at the end I insert it :

 Cours c = new Cours();
                        c.setBranche_cours(selectedBranche);              
                        c.setDate_fin(date2); //should i set the string ?        
                        dbhelper.Open();
                        dbhelper.insertCours(c);

How can I insert in my db the date I selected and not the current date?

@UPDATE - **How can I update the date that is inserted ?

I have another activity, and i want to modify the dates I chose before, but I'm not able..

Here's my sqlite method :

public void updateCours(Date olddatedebut, Date newdatedebut, Date olddatedernier, Date newdatedernier)
{
Open();

db.execSQL("UPDATE "+TABLE_COURS+" set "+COLONNE_DATEPREMIER+"=date('"+newdatedebut+"') where "+COLONNE_DATEPREMIER+"=date('"+olddatedebut+"')");
db.execSQL("UPDATE "+TABLE_COURS+" set "+COLONNE_DATEDERNIER+"=date('"+newdatedernier+"') where "+COLONNE_DATEDERNIER+"=('"+olddatedernier+"')");
}

And how I pass that to my method on my activity:

     //this is the new date of the 2nd datepicker
                        String datedernier = convertDateFormat(datenew2, "yyyy-MM-dd", "dd-MMM-yyyy");
                        //this is the new date of the 1st datepicker
                        String datepremier = convertDateFormat(datenew1, "yyyy-MM-dd", "dd-MMM-yyyy");
                        String date_debutold= intent.getExtras().getString("date_debut");
                        String date_finold=intent.getExtras().getString("date_fin");
                        //this is the current date recorded in my database from my datepicker
                        String date_debut1= convertDateFormat(date_debutold, "yyyy-MM-dd", "dd-MMM-yyyy");
                        //this is the current date recorded in my darabase from my 1st datepicker
                        String date_fin1= convertDateFormat(date_finold, "yyyy-MM-dd", "dd-MMM-yyyy");

                        //nouvelledatedebut
                        Date date_premier= new Date(datepremier);
                        Date date_dernier = new Date(datedernier);
                        Date date_premier2 = new Date(date_debut1);
                        Date date_fin2 = new Date(date_fin1);
                        dbhelper.Open();
                       dbhelper.updateCours(selected_brancheold,selectedBranchenew,date_premier2,date_premier,date_fin2,date_dernier,

Upvotes: 0

Views: 2633

Answers (3)

Tostadora
Tostadora

Reputation: 17

Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:

NULL. The value is a NULL value.

INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.

TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

BLOB. The value is a blob of data, stored exactly as it was input.

There is no DATETIME. SQlite store it as a TEXT. You can't add a day. You have to read it and parse it. And the same goes when you store it. You have to parse it.

Hope it was usefull.

Upvotes: 0

user3613906
user3613906

Reputation:

Thats because you are making some logic on the getter method and setting the new Date(), that will override the date on the date_fin attribute. When you make insertCours probabily this method will try to find all the get methods for the object you are trying to insert. Try change this:

public String getDate_fin() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd-MM-yyyy HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(this.date_fin);
}

to this

public String getDate_fin() {
    return this.date_fin;
}

If you still want to add a format to the Date (String), you can still make it on the getter method, but I don't recommend it.

Upvotes: 2

sajan
sajan

Reputation: 389

try this

public static String convertDateFormat(String date, String curFormat, String desFormat){
    String newFormat = null;
    Date frmtDate = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(curFormat);

        frmtDate = sdf.parse(date);
        SimpleDateFormat formatter = new SimpleDateFormat(desFormat);
        newFormat = formatter.format(frmtDate);

    } catch (Exception e) {
        newFormat = date;
    }
    return newFormat;
}

sample

String result = convertDateFormat(date2, "yyyy-MM-dd", "dd-MMM-yyyy");
c.setDate_fin(result);

Upvotes: 0

Related Questions