参考文章地址:http://bibo531651225.iteye.com/blog/1918112
总结起来代码如下:
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
sdf.setLenient(false);
Date testTime = sdf.parse("2017-12-27 11:26:26.519");
Timestamp ts = new Timestamp(testTime.getTime());
这样就可以将字符串型的时间放到TimeStamp类型当中。
其中,sdf.setLenient(false);的目的是禁止SimpleDateFormat的自动计算功能,如果日期不合格自动报错。lenient意思是宽容的,那么setLenient(false);就是严格界定日期。