如何将字符串转换为MySQL的DateTime或TimeStamp?

时间:2015-05-22 11:31:29

标签: java mysql datetime

m dd yyyy hh:mm:ss
5 12 2015 17:40:53

这需要存储在MySQL表的列中,其数据类型为TimeStamp

1 个答案:

答案 0 :(得分:0)

使用此:

String yourString = "5 12 2015 17:40:53";
Timestamp timestamp = null;

try{
    SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy hh:mm:ss");
    Date parsedDate = sdf.parse(yourString);
    timestamp = new java.sql.Timestamp(parsedDate.getTime());
}catch(Exception e){
 //this generic but you can control the type of exception
}