如何将Java日期存储到Mysql datetime ...? [英] How to store Java Date to Mysql datetime...?
问题描述
任何身体都可以告诉我如何将Java Date存储到Mysql datetime ...?
Can any body tell me how can I store Java Date to Mysql datetime...?
当我试图这样做时,只有日期被存储并且在Mysql日期存储中的时间保持为00:00:00
...
When I am trying to do so...only date is stored and time remain 00:00:00 in Mysql date stores like this...
2009-09-22 00:00:00
我不仅要求日期,还要时间...像
I want not only date but also time...like
2009-09-22 08:08:11
我使用JPA(Hibernate)与spring mydomain类使用java.util.Date,但我已经使用手写查询创建表...
I am using JPA(Hibernate) with spring mydomain classes uses java.util.Date but i have created tables using handwritten queries...
这是我的创建语句
CREATE TABLE ContactUs (id BIGINT auto_increment,
userName VARCHAR(30),
email VARCHAR(50),
subject VARCHAR(100),
message VARCHAR(1024),
messageType VARCHAR(15),
contactUsTime datetime,
primary key(id))
TYPE=InnoDB;
推荐答案
请参阅链接:
http: //www.coderanch.com/t/304851/JDBC/java/Java-date-MySQL-date-conversion
以下代码刚刚解决了问题:
The following code just solved the problem:
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
此 currentTime 插入到类型为DateTime的列中,这是成功的。
This 'currentTime' was inserted into the column whose type was DateTime and it was successful.
这篇关于如何将Java日期存储到Mysql datetime ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!