时间和日期放在我的Sqlite数据库

时间:2014-09-26 17:57:20

标签: android

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}}数据库中。

2 个答案:

答案 0 :(得分:0)

尝试Date对象.toString()。 Android Date对象支持返回字符串表示。

http://developer.android.com/reference/java/util/Date.html

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