Java编程String转Timestamp格式
将String类型的日期字符串转成Timestamp格式并存库
操作方法
- 01
String dateString = "2017/2/16"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/dd"); 定义字符串显示格式
- 02
Date date = null; try { date = sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } 请字符串转成Date数据
- 03
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateTime = sdf2.format(date); 在定义一个新的日期显示格式
- 04
Timestamp finDate = Timestamp.valueOf(dateTime); 将新定义的字符串强转成Timestamp
- 05
完整代码 String dateString = "2017/2/16"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/dd"); Date date = null; try { date = sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateTime = sdf2.format(date); Timestamp finDate = Timestamp.valueOf(dateTime);