• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inserting a date field

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry, figured out where I was wrong.
I should use:

myDate = new java.sql.Timestamp(today.getTime());
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice...patrick
 
reply
    Bookmark Topic Watch Topic
  • New Topic