如何使用JPA将Java日期存储到Mysql日期时间 [英] How to store Java Date to Mysql datetime with JPA
问题描述
有人能告诉我如何将Java日期存储到Mysql日期时间吗??
Can any body tell me how can I store Java Date to Mysql datetime...?
当我尝试这样做时...仅存储日期,而时间保留为00:00:00 这样在Mysql日期存储中...
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.
这篇关于如何使用JPA将Java日期存储到Mysql日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!