Date losing time after retrieval with SQL

P

Peter Grison

I have put the current datetime as a DATETIME field in a mysql database
using NOW() in a sql query. The databse shows the entry as 2004-05-05
23:24:13. However, when I retrieve it through ResultStatement's
getDate() in the time part is lost. When I try to cast it using long to
a new date, the time is set to 00:00:00. What am I doing wrong?

Pete
 
D

Davor Cengija

Peter said:
I have put the current datetime as a DATETIME field in a mysql
database using NOW() in a sql query. The databse shows the entry as
2004-05-05 23:24:13. However, when I retrieve it through
ResultStatement's getDate() in the time part is lost. When I try to
cast it using long to a new date, the time is set to 00:00:00. What
am I doing wrong?

Try with getTimestamp() or maybe getDate() and getTime().
 
G

GaryM

I have put the current datetime as a DATETIME field in a mysql database
using NOW() in a sql query. The databse shows the entry as 2004-05-05
23:24:13. However, when I retrieve it through ResultStatement's
getDate() in the time part is lost. When I try to cast it using long to
a new date, the time is set to 00:00:00. What am I doing wrong?

Pete

Don't know, but ResultSet.getTimeStamp() might work better.
 
B

Bjorn Abelli

...
I have put the current datetime as a
DATETIME field in a mysql database
using NOW() in a sql query. The databse
shows the entry as 2004-05-05 23:24:13.
However, when I retrieve it through
ResultStatement's getDate() in the time
part is lost.

The methods has been created to fit into the ANSI/SQL-standard, which means
that an SQL DATE does not contain the time part. To conform to the
SQL-standard, there are several specialized types created for the different
types in a database.

What getDate() retrieves is a java.sql.Date, which is a subtype to
java.util.Date, but with the time part truncated:

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html

You could try getTimestamp() instead, as it returns another derivation of
Date.

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html


// Bjorn A

P.S. There is a newsgroup comp.lang.java.databases, that probably suits this
type of questions better.
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top