tags:

views:

34

answers:

2

Hello,

I have a datetime field in mysql table and i am using JPA for persisting data but only date goes in database. Time always shows 00:00:00. What should i do?

I am not doing any manipulation with Date. All i do is to assign new Date() to a variable and store it in database.

What am i doing wrong?

+1  A: 

Example below:

private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

Tips like these can be found on http://www.java-tips.org

VoodooChild
Hi VoodooChild, even new Date() by default has time. So there is no need to set the time explicitly. Actually, chris_i is right i had temporaltype.date. Changed it to timestamp and now everything works fine.
Ankit Rathod
ok good, thanks for the feed back
VoodooChild
+2  A: 

Use the annotation @Temporal(TemporalType.TIMESTAMP)

Chris Lercher