GeeCON Prague 2014*
The moose likes JDBC 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
Bookmark "inserting a date field" Watch "inserting a date field" New topic
Author

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();
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
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
Greenhorn

Joined: Oct 05, 2006
Posts: 10
nice...patrick
 
GeeCON Prague 2014
 
subject: inserting a date field