【发布时间】:2015-03-21 14:33:39
【问题描述】:
<tr class="rowsAdded">
<td><input name="item" class="form-control" type="text" placeholder="Item" /></td>
<td><input name="amount" class="form-control" type="number" placeholder="Amount" /></td>
<td><input name="expenseDate" class="form-control" type="date"placeholder="ExpenseDate" /></td>
</tr>
下面是我的控制器和初始化活页夹
@RequestMapping (value = "/saveExpenses", method=RequestMethod.POST)
public String saveExpenses (@RequestBody ExpenseDetailsListVO expenseDetailsListVO, Model model,BindingResult result) {
if (result.hasErrors()) {
System.out.println(result.getFieldError().getField().toString()+" error");
}
System.out.println(expenseDetailsListVO);
return "success";
}
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
这样我想要的日期格式不起作用,这是我得到的输出 expenseDate=2015 年 3 月 18 日星期三 05:30:00 IST 但我希望它变成像 yyyy-MM-dd 这样的特定格式...建议我这样做的方法。
【问题讨论】:
-
我想你的 ExpenseDetailsListVO 类有一个 Date 类型的成员。当您在expenseDetailsListVO 上调用println 时,它只会调用该Date 成员的toString() 方法。您的活页夹不以任何方式参与。因此,您将获得 Date 的默认字符串表示形式。您可以更改打印对象的方式,也可以使用扩展 Date 的新类,但使用不同的 toString() 来存储您的费用日期。
-
是的,你是对的......成员是 Date 类型,因为在后端它也是 date 类型,所以从 VO 转换为 DO 时很容易......所以我现在应该做什么??
-
@Sharique 试试
dateFormat.format(yourDateObj)
标签: java spring spring-mvc