将yyyy-MM-dd hh:mm:ss格式的字符串转化为Date时间类型存储到数据库,就需要使用SimpleDateFormat对象来匹配字符串时间的格式,也是非常容易的,如下所示。
String time = "2017-09-29 17:49:18";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date beginExam = (Date)format.parse(begintime);SimpleDateFormat对象当然也可以将Date类型转换为String时间字符串类型了,代码如下。
Date endTime = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = format.format(endTime);