motiver
motiver

Reputation: 2202

12:00:00 stored as 00:00:00 in MySql when TemporalType.Time is used via Hibernate

I am using Hibernate in my app to store "time" (hours, minutes, seconds). I am using Hibernate to manage my entities. I used the JPA annotation @TemporalType.Time in my entity like below:

@Temporal(TemporalType.TIME)
public Date getTime_out() {
    return time_out;
}

I am storing time that I get as a String from a web service request like this:

            DateFormat df = new SimpleDateFormat("hh:mm:ss");
            Date date = null;
            try {
                date = df.parse(timeHHMM);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

I expect the web request to be in specific format (I know this may not be the right thing to do, but I am just playing around experimenting). After converting the String to Date I set the time on the entity and save it in the database.

When my input is 08:00:00 it stored as 08:00:00 as expected. When it is 13:00:00 (1 PM) it is stored as 13:00:00 which is what I want. But, when my input is 12:00:00 (12 PM) it is stored as 00:00:00.

Am I missing something here?

Thanks!

Upvotes: 1

Views: 745

Answers (1)

MattR
MattR

Reputation: 7048

You're using the wrong date format. hh specifies an hour in am/pm format, so 12:00:00 is defaulting to 12am (00:00:00).

You need:

SimpleDateFormat("HH:mm:ss")

Upvotes: 3

Related Questions