public class item {
private String wishString;
private Date wishDate;
public String getWishString() {
return wishString;
}
public item(String wishString, Date wishDate) {
super();
this.wishString = wishString;
this.wishDate = wishDate;
}
public void setWishString(String wishString) {
this.wishString = wishString;
}
public Date getWishDate() {
return wishDate;
}
public void setWishDate(Date wishDate) {
this.wishDate = wishDate;
}
}
这是我的DatabaseHelper
public class DataBase extends SQLiteOpenHelper {
public static final String DB_NAME = "task_management";
public static final int DB_VERSION = 1;
public static final String EMPLOYEE_TABLE = "employee";
public static final String ID_FIELD = "_id";
public static final String NAME_FIELD = "name";
public static final String TIME_DATE = "time_date";
public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE "
+ EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, "
+ NAME_FIELD + " TEXT, " + TIME_DATE + " DATETIME);";
public DataBase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(EMPLOYEE_TABLE_SQL);
Log.e("TABLE_CREATE", EMPLOYEE_TABLE_SQL);
}
public long insertEmployee(item item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_FIELD, item.getWishString());
values.put(TIME_DATE, item.getWishDate());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
在这一行values.put(TIME_DATE, item.getWishDate());
当我将item.getWishDate()
放置显示错误时Date
String.
Sqlite
如何解决它并将我的日期放在我的{{1}}数据库中。
答案 0 :(得分:0)
尝试Date对象.toString()。 Android Date对象支持返回字符串表示。
答案 1 :(得分:0)
ContentValues
课程中的没有类似put(string,Date)
的方法。因此,您可以将Date
转换为字符串,然后将其存储到db
。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date= sdf.format(item.getWishDate());
values.put(TIME_DATE,date);