我在将表单输入中的datatime插入数据库中时遇到问题。具体来说,我设法解析数据,但是没有解析时间。
我有DATETIME列“ timeFinished”的数据库。
我有JSP输入:
<input type="datetime-local" path="timeFinished" name="timeFinished" id="timeFinished"/>
我有InitBinder控制器:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
在此配置中,日期已成功解析为数据库,如下所示:
1989-12-31 22:00:00
我不知道他在哪里22:00:00,但这就是这样。 但是我也需要解析时间。当我花一些时间到模式时,它不起作用:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
}
在这种情况下,datetime值变为NULL,并且没有任何内容被解析到数据库中。没有错误显示。
如何管理日期和时间到数据库中?我已经尝试了所有可能的模式-hh:mm:ss,HH:MM,HH:MM:SS等。如果我增加时间-它不起作用。如果我只输入日期,它会起作用。
预先感谢您的帮助。
答案 0 :(得分:0)
最后,问题解决了。解决方案:
控制器-将此模式添加到SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
JSP-将此添加到输入中:
pattern="yyyy-MM-dd'T'hh:mm:ss"