Can any body tell me how can I store Java Date to Mysql datetime...?
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
I am using JPA(Hibernate) with spring mydomain classes uses java.util.Date but i have created tables using handwritten queries...
this is my create statement
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;
Are you perhaps using
java.sql.Date
? While that has millisecond granularity as a Java class (it is a subclass ofjava.util.Date
, bad design decision), it will be interpreted by the JDBC driver as a date without a time component. You have to usejava.sql.Timestamp
instead.it works for me !!
in mysql table
in entity:
in process:
in preparedStatement:
see in the link :
http://www.coderanch.com/t/304851/JDBC/java/Java-date-MySQL-date-conversion
The following code just solved the problem:
This 'currentTime' was inserted into the column whose type was DateTime and it was successful.
Annotate your field (or getter) with
@Temporal(TemporalType.TIMESTAMP)
, like this:That should do the trick.
you will get 2011-07-18 + time format