Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserting a date field

 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.sql.Date has a constructor that accepts a long value (epoch), you need to use it.
 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry, figured out where I was wrong.
I should use:

myDate = new java.sql.Timestamp(today.getTime());
 
Sreenivasan M
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice...patrick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic