返回1970-01-01的SQLite数据库

时间:2019-06-06 00:33:44

标签: java android sqlite

我正在开发购物应用程序,并且正在尝试设置用户个人资料功能。用户可以注册一个帐户,并在该帐户中存储一个日期。我使用SQLite数据库进行存储,并且成功存储了正确的日期,但是无论如何存储,它都始终向我返回1970-01-01。

我正在使用java.utils.Date

这是数据库:


private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
        + COLUMN_USER_NO + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + COLUMN_USERNAME + " TEXT," + COLUMN_PASSWORD + " TEXT,"
        + COLUMN_TELEPHONE + " TEXT," + COLUMN_ADDRESS + " TEXT,"
        + COLUMN_CREDITCARD_NO + " INTEGER," + COLUMN_SECURITY_NO + " INTEGER,"
        + COLUMN_EXPIRY_DATE + " LONG," + COLUMN_RATING + " INTEGER, "
        + COLUMN_TOTAL_RATED_BY + " INTEGER" + ")";

这是我检索用户数据的方式:


public User getUser(String username) {

    // array of columns to fetch
    String[] columns = {
            COLUMN_USER_NO,
            COLUMN_USERNAME,
            COLUMN_PASSWORD,
            COLUMN_TELEPHONE,
            COLUMN_ADDRESS,
            COLUMN_CREDITCARD_NO,
            COLUMN_SECURITY_NO,
            COLUMN_EXPIRY_DATE,
            COLUMN_RATING,
            COLUMN_TOTAL_RATED_BY
    };

    String sortOrder = COLUMN_USER_NO + " ASC";

    String selection = COLUMN_USERNAME + " =?";

    String[] selectionArgs = {username};

    SQLiteDatabase db = this.getReadableDatabase();

    // query the user table
    /**
     * Here query function is used to fetch records from user table this function works like we use sql query.
     * SQL query equivalent to this query function is
     * SELECT user_no,user_name,user_email,password FROM user ORDER BY user_name;
     */
    Cursor cursor = db.query(TABLE_USER,
            columns,    //columns to return
            selection,        //columns for the WHERE clause
            selectionArgs,        //The values for the WHERE clause
            null,       //group the rows
            null,
            sortOrder);       //filter by row groups

        if (cursor != null) {
            cursor.moveToFirst();
        }
        User u1 = new User();
        u1.setUserNo(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_USER_NO))));
        u1.setUsername(cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME)));
        u1.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD)));
        u1.setTelephone(cursor.getString(cursor.getColumnIndex(COLUMN_TELEPHONE)));
        u1.setAddress(cursor.getString(cursor.getColumnIndex(COLUMN_ADDRESS)));
        u1.setCreditCardNo(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_CREDITCARD_NO))));
        u1.setSecurityNo(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_SECURITY_NO))));
        u1.setExpiryDate(new Date(cursor.getLong(cursor.getColumnIndex(COLUMN_EXPIRY_DATE))));
        u1.setRating(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_RATING))));
        u1.setTotalRatedBy(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_TOTAL_RATED_BY))));

     return u1;

}


这是我用来从特定用户检索日期的行:


mTextViewProfileCreditCardExpiryDate.setText(user.getExpiryDate());


如何获取正确的存储日期?

0 个答案:

没有答案