gpt4 book ai didi

java - BasicNameValuePair 中的 BasicNameValuePair (String, java.lang.String) 无法应用

转载 作者:行者123 更新时间:2023-11-29 22:12:41 24 4
gpt4 key购买 nike

我目前正在使用此代码在数据库中存储两个字符串(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)

有人可以告诉我如何将日期和时间值存储到数据库中吗?

最佳答案

您可以将日期和时间作为格式化字符串添加到 params

 params.add(new BasicNameValuePair("dtime", reportDate));

处理数据库调用的代码将使用 mysql 函数STR_TO_DATE(...) 将字符串格式化为所需的日期时间。

关于java - BasicNameValuePair 中的 BasicNameValuePair (String, java.lang.String) 无法应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31476173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com