Re: Hibernate, mySQL and formatting dates
On May 22, 1:39 pm, "Alessandro" <n...@nobody.it> wrote:
I'm writing some Date values into datetime fields inside a mySQL table us=
ing
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 i=
n
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.
<http://dev.mysql.com/doc/refman/5.1/en/datetime.html>
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.
4) else
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.
--
Lew