Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Convert java.util.Date to java.sql.Date

Reply
Thread Tools

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

 
 
Bumsys@gmail.com
Guest
Posts: n/a
 
      01-22-2008
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"?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-22-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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"?


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>

--
Lew
 
Reply With Quote
 
 
 
 
GArlington
Guest
Posts: n/a
 
      01-22-2008
On Jan 22, 2:54 pm, (E-Mail Removed) 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"?


1) Make sure that your data fields are defined as DateTimeStamp (or is
it just TimeStamp?)
2) Try to use java.sql.Timestamp
 
Reply With Quote
 
Bumsys@gmail.com
Guest
Posts: n/a
 
      01-22-2008
when I convert java.util.Date to java.sql.Date I want to get sqlDate
in formate "dd-mm-yyyy hh:mm a"???
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-22-2008
(E-Mail Removed) wrote:
> 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.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-22-2008
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Qestion about convert Object to byte[] and convert it back davidxiongcn@gmail.com Java 5 11-04-2006 04:11 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments