Convert java.util.Date to java.sql.Date

B

Bumsys

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"?
 
L

Lew

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"?

The Java Date holds its data as milliseconds since epoch - you have the hours,
minutes and seconds down to the millisecond in there already.

If you are asking how to *display* the Date down to the second, then consider
using things like
<http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html>
or
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html>

If you're asking how to get the database to *store* the date to a certain
resolution, that depends on how you define the column in which you store the
data, which in turn depends on the SQL dialect your DBMS uses. Check the docs
for your DBMS, and if necessary redefine the column to hold the correct
resolution of your data.

The SQL standard defines TIMESTAMP WITH TIME ZONE, which is what you need.
Different DBMSes support it differently. (MySQL is completely non-compliant.
Among other things, it doesn't store time zones.)
<http://troels.arvin.dk/db/rdbms/#data_types-date_and_time>
 
G

GArlington

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"?

1) Make sure that your data fields are defined as DateTimeStamp (or is
it just TimeStamp?)
2) Try to use java.sql.Timestamp
 
B

Bumsys

when I convert java.util.Date to java.sql.Date I want to get sqlDate
in formate "dd-mm-yyyy hh:mm a"???
 
L

Lew

when I convert java.util.Date to java.sql.Date I want to get sqlDate
in formate "dd-mm-yyyy hh:mm a"???

java.sql.Date does not *have* "formats". As I mentioned, it only stores
milliseconds since epoch. That means it already contains the time down to the
millisecond.

Review my recommendation about java.text.DateFormat. Did you consider the
information I provided upthread?

GArlington mentioned java.sql.Timestamp. Aside from its more natural match to
the SQL TIMESTAMP type, Timestamp holds time to nanosecond resolution. Just
like java.util.Date and its other offspring, java.sql.Date, Timestamp also
does not have a "format" regarding "hh:mm", etc.

Sidebar: The Javadocs refer to the "precision of a Timestamp object" in terms
of the number of characters in its String representation. That is bogus.
Like its parent, java.util.Date, Timestamp holds long values that represent
milliseconds (actually seconds) and nanoseconds since epoch. Its precision
therefore is nanoseconds. I don't know what they were thinking when they
wrote that part about "precision" being "19" - not even any units specified.

Much as I am in favor of the Javadocs, occasionally they disappoint.

Anyway, OP, for your purposes remember that java.util.Date and its offspring
DO NOT HAVE FORMATS for the date. The only "format" they have is one or two
long values.

Please review the advice given earlier.
 
L

Lew

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.)
 

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

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top