Re: Convert java.util.Date to java.sql.Date
Bum...@gmail.com wrote:
I convert java.util.Date to java.sql.Date this way:
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
And then send sqlDate to prepareStatement.
ps.setDate(1, sqlDate).
But I got the date such as dd-mm-yyyy. And I need also take hours,
minutes pm(am).
How can I get the Date "dd-mm-yyyy hh:mm a"?
GArlington wrote:
1) Make sure that your data fields are defined as DateTimeStamp (or is
it just TimeStamp?)
The SQL standard specifies TIMESTAMP. Some DBMSes deviate from the standard.
(MySQL in significant ways. Its "TIMESTAMP" is not even remotely like the
SQL standard type.)
2) Try to use java.sql.Timestamp
This is good advice,. According to the Javadocs, java.sql.Date is intended to
match with the SQL DATE type, which has date resolution, much less than the
resolution of the Java type. java.sql.Timestamp matches with the SQL
TIMESTAMP type, which has fractional-second resolution like the Java type.
(Except in MySQL.)
This could be the source of your problem, OP. It could be that the JDBC
connection is dropping the hours/minutes/seconds/fractions part in the
conversion to a SQL type. You have not given any information about the SQL
data type involved, what the DMBS is, what the code that sets the Java value
looks like or anything else like that that would help us be more exact in our
answers.
java.text.DateFormat (and its offspring) should work just fine on
java.sql.Timestamp. (Bear in mind that "[o]nly integral seconds are stored in
the java.util.Date component" of java.sql.Timestamp.)
--
Lew