File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes java.util.Date to java.sql.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "java.util.Date to java.sql.Date" Watch "java.util.Date to java.sql.Date" New topic

java.util.Date to java.sql.Date

Rishi Upadhyay

Joined: Oct 05, 2000
Posts: 6
I have to insert a date in the database(oracle 8).I am using following peice of code:
if(info.getLastUpdatedTime() == null )
System.out.println("if:UPDTime in ejbcreate oucbean....");
pstmt.setDate(7,date);//date is defined as java.sql.Date
System.out.println("else:UPDTime in ejbcreate oucbean....");
The method getLastUpdatedTime() is defined to return a a java.util.Date.But I have to convert it to java.sql.Date before I issue an insert sql statement.
Can anybody give me a peice of code that converts java.util.Date to java.sql.Date.
(weblogic 5.1.0
oracle 8)
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
You can construct a new java.sql.Date object from the getTime() method of java.util.Date.

For your example

Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rishi Upadhyay

Joined: Oct 05, 2000
Posts: 6
Thanks for the input Carl.Its working fine now and I can insert records in the database.
ammuswar kumar

Joined: Aug 19, 2001
Posts: 27

i have a table in SQL-SERVER named as title
this table has three fields
2.tdate----this is of the type datetime
i am using JRUN 3.0
i want to make a java servlet in which u have a text link
Date todaysdate = new Date();
todays content;
In the servlet :
i am accepting this parameter as
String a = request.getParameter("todaysdate");

i will the database connecion---------------------
select * from title where tdate = 'a';

-----------but this not showing any result
there is something wrong here in he compatitbility of 'a' with tdate
klein bottle

Joined: Oct 28, 2003
Posts: 1
if using JDBC-ODBC instead of pure JDBC it seems you must explicitly format the date
// How to put a date into a sql database. 29 October 2003
//This is a repost, but theres a lot of suggestions out there,
// This is the one that works (took me half a day to find that out)
// I use sun.jdbc.odbc.JdbcOdbcDriver. (because my JB9 hates pure jdbc)
// This is the ONLY method that works for me
// original: Eddy Young Coding_MU

java.sql.Date sqldate (String dmy ) // dd/MM/yyyy to java.sql.Date
SimpleDateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d;
try {
d = dF.parse(dmy);
dmy = dF.format(d);
} catch (Exception e) {
return java.sql.Date.valueOf(dmy);
} //sqldate_______________________________________________________________
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

If I remember correctly, the JDBC-ODBC bridge driver (in J2SE 1.4.2) supports the SQL escape syntax
Good Luck,
I agree. Here's the link:
subject: java.util.Date to java.sql.Date
It's not a secret anymore!