The moose likes JDBC and Relational Databases and the fly likes inserting a date field 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 "inserting a date field" Watch "inserting a date field" New topic

inserting a date field

Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
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();
myDate = new java.sql.Date(shortDateFormat.parse(date).getTime());
catch(Exception e){

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

Joined: Apr 17, 2006
Posts: 23
The java.sql.Date has a constructor that accepts a long value (epoch), you need to use it.

SCJP 1.4 SCWD 1.4
Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
Don't worry, figured out where I was wrong.
I should use:

myDate = new java.sql.Timestamp(today.getTime());
Sreenivasan M

Joined: Oct 05, 2006
Posts: 10
I agree. Here's the link:
subject: inserting a date field
It's not a secret anymore!