问题
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