1.把带有秒的时间的写入数据库。
Date date=new Date();
pstmt.setTimestamp(下标,new Timestamp(date.getTime()));
2.读出数据库的时间给string的变量,还用到了简单时间格式化
Timestamp timestamp = rs.getTimestamp(下标);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date=sdf.format(new Date(timestamp.getTime()));
或者另一种方法:先new一个时间对象,再格式化为String存入数据库中,数据库读取出来时,以String方式存储即可。
//存入代码
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//从前端或者自己模拟一个日期格式,转为String即可
String dateStr = format.format(date);