@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new MyDateEditor());
}
private class MyDateEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
setValue(date);
}
}
前端传递的是时间戳,在后端接收参数时转换成UTC时间
最新推荐文章于 2025-09-25 02:33:57 发布
本文介绍如何在Spring MVC框架中使用@InitBinder注解自定义日期编辑器,通过注册MyDateEditor来解析和设置日期格式为'yyyy-MM-dd HH:mm:ss',确保日期数据的正确处理。

5万+

被折叠的 条评论
为什么被折叠?



