Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentValues put method

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,

like image 793
user182944 Avatar asked Aug 31 '26 21:08

user182944


1 Answers

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));
like image 104
waqaslam Avatar answered Sep 03 '26 11:09

waqaslam