简体   繁体   中英

how to insert date in SqLite Database in android

i want to insert date in Sqlite database i'm stuck at this point. I have taken Date datatype in my pojo class. now i want to insert that thing in my Sqlite Databse using contentvalue so what should i do. it is like this.

 SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    String a = String.valueOf(dp.getDob());
    cv.put(DOB, a);

in pojo class

Date dob;

public DoctorPojo( String name,Date dob,String gender, String adder,String city,
                   String cas,String mobile, Date date,String disease)
{
    this.name = name;
    this.dob = dob;
    this.gender = gender;
    this.adder = adder;
    this.city = city;
    this.cas = cas;
    this.mobile = mobile;
    this.date = date;
    this.disease = disease;
}

According to the Sqlite specficication https://www.sqlite.org/lang_datefunc.html , You have to store Date as String with format YYYY-MM-DD.

It's easy to do that with SimpleDateFormatter.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(dp.getDob());
cv.put(DOB, formattedDate );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM