【发布时间】:2015-07-17 12:46:50
【问题描述】:
我目前正在使用此代码在数据库中存储两个字符串(newCategory 和 reportDate):
String newCategory = spinnerFood.getSelectedItem().toString();
// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String reportDate = df.format(today);
// Preparing post params
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", newCategory));
params.add(new BasicNameValuePair("dtime", reportDate));
ServiceHandler serviceClient = new ServiceHandler();
String json = serviceClient.makeServiceCall(URL_NEW_CATEGORY,
ServiceHandler.POST, params);
这将成功存储名称字符串。但是数据库中的日期和时间数据包含值:
0000-00-00 00:00:00
如果我将日期字符串更改为日期/时间格式,如下所示:
df.format(today);
params.add(new BasicNameValuePair("dtime", today));
我得到错误:
BasicNameValuePair中的BasicNameValuePair(String, java.lang.String)不能应用
到(字符串,java.util.date)
谁能告诉我如何将日期和时间值存储到数据库中?
【问题讨论】:
-
Timestamp存储日期和时间。 -
在这种情况下如何使用 Timestamp?
-
查看我的更新答案。
-
无效的
DATE、DATETIME或TIMESTAMP值将转换为相应类型的zero值。就像上面你说的那样。 @西林 -
好的。但是这段代码中的 rs 和 index 代表什么:
java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(index);//now retrieve here抱歉,我是 java 新手..