ContentValues put method

让人想犯罪 __ 提交于 2020-01-01 06:48:45

问题


At the db side, checkInTime and checkOutTime are of type TIMESTAMP

In my java code also, checkInTime and checkOutTime are of type java.sql.Timestamp

For inserting a record into db, i am using this piece of code:

1. ContentValues contentValues=new ContentValues();

    2. contentValues.put(USER_ID, userId);
    3. contentValues.put(ROOM_ID, roomId);
    4. contentValues.put(CHECK_IN, checkInTime);
    5. contentValues.put(CHECK_OUT, checkOutTime);

    6. return database.insert(MRM_BOOKING_DETAILS_TABLE, null, contentValues);

But i am getting compilation errors at line 4 and 5 since they are not expecting something of type java.sql.Timestamp

I can't see any put method of ContentValues which will accept type java.sql.Timestamp in its second parameter. Please suggest how to pass the java.sql.Timestamp in such a case so that i can remove the compilation errors as well.

Thanks,


回答1:


I would suggest you to use DATETIME instead of TIMESTAMP. Its more general when handling dates and times.

Moreover, you can use SimpleDateFormat to parse DATETIME values when inserting or reading from database.

final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Updated:

Try doing it as below:

final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSS");

contentValues.put(CHECK_IN, parser.format(checkInTime));
contentValues.put(CHECK_OUT, parser.format(checkOutTime));



回答2:


If checkInTime and checkOutTime are bith time stamps then before storing thm to contentvalue just convert to milliseconds and store them and while retriving reconvert then them to your required format. because ContentValue dont accept timeStamps so just convert and store so that you wont get any compliation error.



来源:https://stackoverflow.com/questions/10160968/contentvalues-put-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!