I have a Date that has been converted to a String to be passed to a Java Socket server. An example of this value is 06:19:18p.m. 13/01/2011.
Within the Socket server I’m trying to convert it back to a Date value before writing it to a SQL Database table, but the converted value is NULL. I have also tried writing the value to the SQL database as the original String, but it doesn’t write unless there are no colon, full stop, or forward-slash characters in it.
Is there any way I can get around this? I’m sorry that I can’t view or post the stack trace, as I don’t have admin access to the server I’m running my Jar file on.
My code to convert the String is:
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ssa dd/MM/yyyy");
try {
date = sdf.parse(timestamp);
}
catch (ParseException e) {
e.printStackTrace();
}
I am not sure why you can’t parse correctly but you are thinking all wrong about this. Instead of saving String in the database you should make the column of type TIMESTAMP or DATE. Then you can use getDate() from the JDBC to just get the date object.