【问题描述】
最近在写代码的时候发现了个问题就是从键盘中输入一个日期然后通过SQL语句插入到数据库中发现数据库中的日期与我的日期不一样,例如我输入的是:
>1995-08-09
但是数据库显示的确一直是
>1995-01-01
【寻找原因】
既然问题出现了,那么就一定是某个环节出了问题,于是我各种测试,最终发现是我的日期输入的格式转换有问题
【问题诊断】
因为我就是练习一下数据库的简单操作,所以设置了个实体类,里面的元素brithday设置为Date类型
import java.util.Date;
public class User {
private int id;
private String name;
private String password;
private String email;
private Date brithday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public User( String name, String password, String email, Date brithday) {
super();
this.name = name;
this.password = password;
this.email = email;
this.brithday = brithday;
}
public User() {
super();
}
public User(int id, String name, String password, String email, Date brithday) {
super();
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.brithday = brithday;
}
@Override
public String toString() {
return "id=" + id +" "+ "姓名=" + name + " "+"密码=" + password + " "+"邮箱=" + email + " "+"生日="
+ brithday ;
}
}
所以不能像网上他们那样设置为String,直接可以转换的,因此难点也就在这里,也就是我之前想当然的以为直接输入字符串而没有转换,结果就出问题了。
其次是我没有修改前输入日期的那一段代码
String bs = sc.next();//输入日期以字符串的格式输入
Date brithday = df.parse(bs);//将String转换为Date
运行时候数据库显示的错误结果
请输入生日:
1998-12-27
添加用户成功!
请输入您的 选择:
1.添加用户
2.删除用户
3.修改用户
4.查询用户
5.查询所有用户
5
========有以下用户========
id=33 姓名=5 密码=4 邮箱=4 生日=1999-12-27
【解决方案】
>首先我们要了解一下Date和String之间的转换
>java中String和Date的互相转换使用SimpleDateFormat来完成。SimpleDateFormat使用记得 import java.text.SimpleDateFormat。
例如:
>Date--->String
> String =(new SimpleDateFormat("格式")).format(Date);
java.text.SimpleDateFormat formatter =
new
SimpleDateFormat(
"yyyy-MM-dd "
);
String s=
"2011-07-09 "
;
Date date = formatter.parse(s);
>String--->Date
> Date = (new SimpleDateFormat("格式")).parse(String);
java.text.SimpleDateFormat formatter =
new
SimpleDateFormat(
"yyyy-MM-dd "
);
String date = formatter.format(
new
Date());
//格式化数据
顺便补充下SimpleDateFormat 语法:
G -- 年代标志符
y -- 年
M -- 月
d -- 日
h -- 时 12小时制 (1~12)
H -- 时 24小时制 (0~23)
m -- 分
s -- 秒
S -- 毫秒
E -- 星期
D -- 一年中的第几天
F -- 一月中第几个星期几
w -- 一年中第几个星期
W -- 一月中第几个星期
a 上午 / 下午 标记符
k 时 在一天中 (1~24)
K 时 在上午或下午 (0~11)
z 时区
>接下来就是成功解决问题
String bs = sc.next();
Date brithday = DateFormat.getDateInstance(DateFormat.SHORT).parse(bs);
顺便补充下java怎么格式化时间日期,这里不一一详细解释,附带一链接
>再次运行
请输入生日:
1997-12-27
5添加用户成功!
请输入您的 选择:
1.添加用户
2.删除用户
3.修改用户
4.查询用户
5.查询所有用户
========有以下用户========
id=46 姓名=1 密码=1 邮箱=1 生日=1997-12-27
这样问题就解决了
【注意】
我这里因为是把日期插入到数据库,所以还有操作数据库那里也需要改变一下,我就把我一个添加方法加进来
public boolean add(User user) throws Exception {
Connection conn = JDBCUtil.getConnection();
int res = 0;
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
String sql = "insert into user(name,password,email,brithday) values(?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
ps.setString(4,sdf.format(user.getBrithday()));
res = ps.executeUpdate();
return res == 0 ? false : true;
}
问题到这里就差不多结束了,所以说编程的路上困难重重我们还需要一个不畏困难勇往直前的心态去面对,这样我们才能走的更远,加油!!