上次发现错误之后,自己找了很多更改日期的方法,发现还是有各种问题,在当天晚上的调试中,意外发现,它会把每次获取到时间的,分钟,赋予给月份。
而在之后的调试查询中。发现问题的根源在于,我在写dao层的时候,因为疏忽。把 yyyy-MM-dd 中MM写成了小写,小写mm表示的是分钟。
所以,无论怎么获取日期都会出问题。附上我dao的部分代码。
public class UsersDao {
public boolean insert(User user)
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
stmt = conn.createStatement();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//此处出的问题 //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
String birthday = sdf.format(user.getBirthday());
int id = user.getId();
String name = user.getUsername();
String password = user.getPassword();
String email = user.getEmail();
String sql = "INSERT INTO users(id,name,password,email,birthday)"+
"VALUES("+id+",'"+name+"','"+password+"','"+email+"','"+birthday+"')";
int num = stmt.executeUpdate(sql);
if(num>0)
{
return true;
}
return false;
}
大家可以看到,注释掉的那一行,是我之前写的。所以才会无论怎么调试都是错误。
按照这种写法,最开始的事例也是正确的。
不管怎么说,就当是学习了各种日期转换的方法。