ContentValues​​把方法 [英] ContentValues put method

查看:127
本文介绍了ContentValues​​把方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库端, checkInTime checkOutTime 的数据类型为 TIMESTAMP

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

在我的Java code也, checkInTime checkOutTime 的数据类型为的java.sql.Timestamp

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

有关插入一条记录到数据库中,我使用这片code:

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

但我得到的编译错误,在4号线和5,因为他们不指望什么类型的java.sql.Timestamp的

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

我不能请参阅C ontentValues​​ 将接受任何此类put方法的java.sql.Timestamp 的它的第二个参数。请建议如何通过在的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.

谢谢

推荐答案

我会建议你使用 DATETIME 而不是 TIMESTAMP 。它更普遍的处理日期和时间时。

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

此外,还可以使用SimpleDateFormat的插入或从数据库中读取数据时解析DATETIME值。

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");


更新:

试着做如下:

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

这篇关于ContentValues​​把方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆