如何在android中的SqLite数据库中插入日期

时间:2016-04-12 17:47:09

标签: android android-sqlite

我想在Sqlite数据库中插入日期我已经陷入困境。我在我的pojo类中使用了Date数据类型。现在我想使用contentvalue在我的Sqlite数据库中插入那个东西,所以我该怎么办。就像这样。

 SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    String a = String.valueOf(dp.getDob());
    cv.put(DOB, a);
在pojo课程中

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

1 个答案:

答案 0 :(得分:1)

根据Sqlite规范https://www.sqlite.org/lang_datefunc.html,您必须将日期存储为字符串,格式为YYYY-MM-DD。

使用SimpleDateFormatter很容易做到这一点。

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