我想在Sqlite数据库中插入日期我已经陷入困境。我在我的pojo类中使用了Date数据类型。现在我想使用contentvalue在我的Sqlite数据库中插入那个东西,所以我该怎么办。就像这样。
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
String a = String.valueOf(dp.getDob());
cv.put(DOB, a);
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;
}
答案 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 );