我有这样的输入
<input type="date" name="date">
如何从这个输入读取类java.util.Date的java对象?
P.S。日期日期是我的Bean的一个字段,我这样读过:
@RequestMapping("/updateVacancy")
public String updateVacancy(@ModelAttribute("vacancy") Vacancy vacancy){
vacancyService.update(vacancy);
return "VacancyDetails";
}
答案 0 :(得分:4)
您可以以文本形式接收日期,然后将其解析为java.util.Date
例如像这样
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date parsed = format.parse(date);
您还应该检查收到的值是否符合您所需的格式。
答案 1 :(得分:0)
value = date
表示日期的字符串。 [RFC 3339]中定义的有效完整日期,附加限定年份组件是四位或更多位数,表示大于0的数字。
示例:
1996年12月19日
所以你必须根据这种格式解析参数值。在服务器端,您将获得参数值,就像该字段是text类型的输入一样,其值是使用yyyy-MM-dd
模式格式化的日期。
答案 2 :(得分:0)
根据建议here,您应该在控制器中声明一个@InitBinder,该@InitBinder处理从字符串中解析Date对象:
/* put this in your Controller */
@InitBinder
private void dateBinder(WebDataBinder binder) {
//The date format to parse or output your dates
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
//Create a new CustomDateEditor
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
//Register it as custom editor for the Date type
binder.registerCustomEditor(Date.class, editor);
}