Java在mysql資料庫中插入datatime時間資料
阿新 • • 發佈:2019-02-20
//將時間字串轉Timestamp型別
public static Timestamp string2Time(String dateString) throws ParseException {
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
dateFormat.setLenient(false);
java.util.Date timeDate = dateFormat.parse(dateString);//util型別
Timestamp dateTime = new Timestamp(timeDate.getTime());//Timestamp型別,timeDate.getTime()返回一個long型
return dateTime;
}
Timestamp t = string2Time("2018-07-27 12:25:00");
//組織sql語句
String sql = "insert into table values('"+t+"');";