A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Databases
»
JDBC
Author
inserting a date field
Patrick Mugabe
Ranch Hand
Joined: Jan 08, 2002
Posts: 132
posted
Jun 26, 2006 04:55:00
0
I want to insert the current Date into my table.
I need to convert a
java.util.Date
to a
java.sql.Date
. The date in my database colum is of type Timestamp.
java.util.Date
today = new
java.util.Date
();
SimpleDateFormat
shortDateFormat = new
SimpleDateFormat
("dd/MM/yyyy");
String
date = today.toString();
try{
myDate = new
java.sql.Date
(shortDateFormat.parse(date).getTime());
}
catch(Exception e){
e.printStackTrace();
}
On trying to insert I get this error:
java.text.ParseException
: Unparseable date: "Mon Jun 26 12:46:54 GMT+02:00 2006"
Mouly Kumaraswamy
Greenhorn
Joined: Apr 17, 2006
Posts: 23
posted
Jun 26, 2006 05:09:00
0
The
java.sql.Date
has a constructor that accepts a long value (epoch), you need to use it.
java.util.Date date = new java.util.Date(); java.sql.Date date2 = new java.sql.Date(date.getTime());
SCJP 1.4 SCWD 1.4
Patrick Mugabe
Ranch Hand
Joined: Jan 08, 2002
Posts: 132
posted
Jun 26, 2006 05:11:00
0
Don't worry, figured out where I was wrong.
I should use:
myDate = new
java.sql.Timestamp
(today.getTime());
Sreenivasan M
Greenhorn
Joined: Oct 05, 2006
Posts: 10
posted
Oct 05, 2006 12:17:00
0
nice...patrick
I agree. Here's the link:
http://aspose.com/file-tools
subject: inserting a date field
Similar Threads
Inserting the Date in to MySQL
converting a string into a date
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Opti onal fature not implem
JSP and SQL problem
Java date to MySQL date conversion
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter