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;
Actually you may not use SimpleDateFormat, you can use something like this;
This way you can directly get the date with format as specified.
Its very simple though conditions in this answer are in mysql the column datatype is datetime and you want to send data from java code to mysql:
java.util.Date dt = new java.util.Date();
whatever your code object may be
.setDateTime(dt);Probably because your java date has a different format from
mysql format
(YYYY-MM-DD HH:MM:SS
)do this
Use the following code to insert the date into MySQL. Instead of changing our date's format to meet MySql's requirement, we can help data base to recognize our date by setting the
STR_TO_DATE(?, '%l:%i %p')
parameters.For example, 2014-03-12 can be represented as
STR_TO_DATE('2014-03-12', '%Y-%m-%d')
mysql datetime -> GregorianCalendar
GregorianCalendar -> mysql datetime