解决方案:
1、局部转换
- //只需要加上下面这段即可,注意不能忘记注解
- @InitBinder
- public void initBinder(WebDataBinder binder, WebRequest request) {
- //转换日期
- DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
- }
2、全局转换
创建CustomDate类实现WebBindingInitializer
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.springframework.beans.propertyeditors.CustomDateEditor;
- import org.springframework.web.bind.WebDataBinder;
- import org.springframework.web.bind.support.WebBindingInitializer;
- import org.springframework.web.context.request.WebRequest;
- /**
- * @作者:JackHisen(GWD)
- * @项目名:shoppingmall
- * @时间:2017-7-12 下午10:39:10
- * @version 1.0
- */
- public class CustomDate implements WebBindingInitializer{
- @Override
- public void initBinder(WebDataBinder binder, WebRequest request) {
- // TODO Auto-generated method stub
- //转换日期
- DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
- }
- }
在Spring-MVC.xml中配置日期转换
- <!-- 日期转换 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="webBindingInitializer">
- <bean class="com.gwd.shopping.core.web.CustomDate"/>
- </property>
- </bean>