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 forwar开发者_C百科d-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.
The "a" in your SimpleDateFormat pattern can only parse "AM" or "PM", but not "p.m.". Just change that and your parsing will be successful.
This might just be a typo but your example of "06:19:18p.m. 13/012011" does not match your simple date format because it expecting two slashes in your date.
It would be very useful to find a way to get the stack track or try this code out on the data separately elsewhere so that you can make sure you aren't just getting bad data.
精彩评论