如何在不丢失时间部分的情况下在 Java 中将日期作为 SQL 参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23997301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass a Date as SQL parameter in Java without losing the time part?
提问by n00b1990
For example, I have this String: 06/10/2013 18:29:09. I want to convert this string and put it in a SQL database (as Date).
例如,我有这个字符串:06/10/2013 18:29:09。我想转换这个字符串并将其放入 SQL 数据库(作为日期)。
How can I convert this to an sql date, so that it could be inserted into a database? I want the hours minutes and seconds to remain as well.
如何将其转换为 sql 日期,以便将其插入到数据库中?我希望小时分和秒也保留下来。
I tried the following:
我尝试了以下方法:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date javaDate = sdf.parse("06/10/2013 18:29:09");
java.sql.Date date = new java.sql.Date(javaDate.getTime());
采纳答案by Luiggi Mendoza
The problem is here:
问题在这里:
java.sql.Date date = new java.sql.Date(javaDate.getTime());
java.sql.Date
stores the date part of your timestamp. If you want/need to handle both date and time, use java.sql.Timestamp
instead:
java.sql.Date
存储时间戳的日期部分。如果您想要/需要同时处理日期和时间,请java.sql.Timestamp
改用:
java.sql.Timestamp date = new java.sql.Timestamp (javaDate.getTime());
More info:
更多信息:
回答by Marcelo Tataje
If you are working with String type for date input and then you want to save that in a database like MySQL, you should use an appropriate Date Format for it. There's a class called "SimpleDateFormat" which you can use for that purpose. You can find a sample in the following link, also a brief explanation on how it works. Hope it helps.
如果您使用 String 类型进行日期输入,然后您想将其保存在 MySQL 等数据库中,则应为其使用适当的日期格式。有一个名为“ SimpleDateFormat”的类,您可以为此目的使用它。您可以在以下链接中找到示例,以及有关其工作原理的简要说明。希望能帮助到你。
Example:http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm
示例:http : //www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm
Best Regards.
此致。
回答by NRJ
You will use a SimpleDateFormat object to parse the string to java.util.date and then use the getTime() method to instantiate a java.sql.Date.
您将使用 SimpleDateFormat 对象将字符串解析为 java.util.date,然后使用 getTime() 方法实例化 java.sql.Date。
String input = "06/10/2013 18:29:09";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
java.util.Date dt = sdf.parse(input);
java.sql.Date dtSql = new java.sql.Date(dt.getTime());
回答by Olu Smith
Here's a simple demo. In a Database table like this.
这是一个简单的演示。在这样的数据库表中。
You can insert into it like this.
你可以像这样插入它。
//the SQL statement for creating the database table
create table user(id, integer primary key, username varchar(100), date_created varchar(100));
//the java code to insert into the table created above.
try{
String date = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss").format(new Date());
String sql = "insert into user(username, date_created) values('olupotd', '"+date+"')";
int done = statement.executeUpdate(sql);
if(done > 0)
//inserted
else
//not inserted.
}catch(java.sql.SQLException e){}
Hope that helps
希望有帮助