Forums Register Login

inserting a date field

+Pie Number of slices to send: Send
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"
+Pie Number of slices to send: Send
The java.sql.Date has a constructor that accepts a long value (epoch), you need to use it.
+Pie Number of slices to send: Send
Don't worry, figured out where I was wrong.
I should use:

myDate = new java.sql.Timestamp(today.getTime());
+Pie Number of slices to send: Send
nice...patrick
moose poop looks like football shaped elk poop. About the size of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1237 times.
Similar Threads
converting a string into a date
Inserting the Date in to MySQL
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Opti onal fature not implem
Java date to MySQL date conversion
JSP and SQL problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:44:37.