0

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,

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought in using String instead of datetime values, but after, I need to compare those 2 dates..And then I have 3 listviews, and I must sort by date.. But is it possible to do it even if I insert it as a String ?
Also, that means I have to change the type of column right ? String instead of Datetime ?
@David, use the attribute as String or Date/Datetime/Calendar is up to you. As Tostodora said, SQLite doesn't have any datetime type, so when inserting in the DB you could use the date as String (like you done), or integers/numbers. A straight foward option is, your attribute date is a String type one and when you need two compare the date with other one, you convert it to a Date object.
0

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);

8 Comments

sorry for asking, but I create that method in my activity ?
if its in your activity no need of static use private String convertDateFormat
are you getting the required correct date in "date2" after selecting it from the datepicker?
have you tried niarb's answer? , getDate_fin returns current date
@ankitpurwar, yes when I select a date, I show it in a TextView and it shows currectly, but when I add , if I check again, it shows the current date..
|
0

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.

2 Comments

So I should store as a String value ?
I always store it as TEXT. If you declare it in create table as datetime, acording to this sqlite.org/datatype3.html it will be stored as numeric. Edit: Store it as TEXT but when ever you read it, parse it to DATE. It should be treated as a DATE in your Java code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.