春天@InitBinder为joda时间

时间:2013-12-18 17:41:54

标签: spring spring-mvc jodatime

我有一个java.util.Date的初始化器:

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            Constants.DATE_PICTURE);
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, true));

用什么来绑定joda时间LocalDateTime? 我正在使用Spring 3.1

1 个答案:

答案 0 :(得分:5)

当您使用命令对象时,可以使用完全不同的,更优雅的方法:使用@DateTimeFormat注释。

public class CommandObject {
    @DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss a")
    public Date myDate();        
}

@Controller
public class ControllerClass{
   @RequestMapping("/myUrl")
   public ModelAndView(CommandObject c) {...}
}

(@有关示例格式化程序,请参阅this blog