错误方式
Date date = new Date();
long dateTime = date.getTime();
String p = "yyyy-MM-dd HH:mm:ss";
Locale locale = Locale.getDefault();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(p, locale);
String format = simpleDateFormat.format(dateTime);
Date parse = null;
try {
parse = simpleDateFormat.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date date1 = new java.sql.Date(parse.getTime());
这样获取的date1传入数据库时会出现时分秒精度丢失
解决方法
// java.sql.Date date1 = new java.sql.Date(parse.getTime());
Timestamp timestamp = new Timestamp(parse.getTime());
使用java.sql下的Timestamp,存入的Date数据精度不会丢失。