Hibernate, mySQL and formatting dates

A

Alessandro

Hello,

I'm writing some Date values into datetime fields inside a mySQL table using
hibernate (in the mapping file all that fields were declared as "date").

Everything works fine but I view also hh:mm:ss and I wouldn't view that in
the mySQL table.

The Date object has been formatted using SimpleDateFormat ("yyyy-MM-dd"),
time information shouldn't be valorized but then setting in the table I view
also time as 00:00:00.

Then do I need to set in some way tha date format:

1) in mySQL
2) somewhere in hibernate
3) transform datetime field into string and map it as "string", but this is
a poor workaround
4) else


Thanks all and best regards,
Ale
 
L

Lew

I'm writing some Date values into datetime fields inside a mySQL table using
hibernate (in the mapping file all that fields were declared as "date").

The 'Date' component of a 'java.sql.Timestamp' holds time information
to a resolution of one second. 'java.util.Date' itself has one-
millisecond resolution.

The MySQL DATETIME type holds time information to a resolution of one
second.
Everything works fine but I view also hh:mm:ss and I wouldn't view that in
the mySQL table.

Then format your view differently. You are seeing the data type with
the resolution that it supports.
The Date object has been formatted using SimpleDateFormat ("yyyy-MM-dd"),

'Date' objects don't get formatted. Formatting happens only to
'String' values.
time information shouldn't be valorized

What does 'valorized' mean?
but then setting in the table I view also time as 00:00:00.

Because that is what you set the time portion of the 'Date' to.
Then do I need to set in some way tha date format:

1) in mySQL

You might consider defining the column as 'DATE' type. Consider
reading the MySQL documentation.
2) somewhere in hibernate
No.

3) transform datetime field into string and map it as "string", but this is
a poor workaround

Bear in mind that Java's 'Date' and 'Timestamp' types are binary types
- they do not get formatted.

The same is true of MySQL's (non-standard) 'DATE', 'DATETIME' and
'TIMESTAMP' types.

You are storing the values exactly as you told the system to, as
'DATETIME' types with the hour/minute/second portion zeroed out.
These binary types do not get formatted; the concept is meaningless.
You can format the output from your MySQL query differently if you
wish; that is an operation performed on the string representation of
the 'DATETIME' binary value.
 
A

Arne Vajhøj

Lew said:
The 'Date' component of a 'java.sql.Timestamp' holds time information
to a resolution of one second. 'java.util.Date' itself has one-
millisecond resolution.

That is correct, but it has another component that stores fractions
of seconds.

Arne
 
L

Lew

That is correct, but it has another component that stores fractions
of seconds.

However, MySQL 'DATETIME' does not, so the extra resolution is wasted
there. The OP's problem related to seeing a resolution finer than one
day.

The central point is that each binary data type has a certain
resolution, be it seconds, milliseconds or nanoseconds, and no
formatting applies, nor is that resolution altered by any formatting
applied to the string representation of that binary type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top