如何使用 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
我不仅要日期,还要时间...like
I want not only date but also time...like
2009-09-22 08:08:11
我在 spring mydomain 类中使用 JPA(Hibernate) 使用 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)
);
推荐答案
见链接:
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屋!