1.局部日期转换
@Controller public class ProductController{ @RequestMapping(value="/test/springmvc.do") public String test(String name,Date birthday){ System.out.println(name+birthday); return ""; } //局部性的转换 @InitBinder 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)); } }
2.全局性的时间转换
1).在springmvc.xml添加配置
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <!-- 日期格式转换 --> <property name="webBindingInitializer"> <bean class="cn.itcast.core.web.DateConverter" /> </property> </bean>
2).编写 DateConverter
public class DateConverter implements WebBindingInitializer { 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)); } }