Custom Date Formatting with Spring Data Binders

It's relatively trivial to modify the way dates are formatted when they are bound to form fields with Spring. Just implement the initBinder method in your controller. Here's an example.

protected void initBinder(HttpServletRequest request,
                          ServletRequestDataBinder binder)
  throws Exception
{
  super.initBinder(request, binder);
  binder.registerCustomEditor(Date.class,
    "estimatedCompletionDate",
    new CustomDateEditor(new SimpleDateFormat( "M/d/yy"), true)
  );
}

Leave a comment


Type the characters you see in the picture above.

Who's this guy?

Aaron Longwell is Chief Web Craftsman at New Media Logic Corporation in Coeur d' Alene, Idaho. As a professional software developer for 12 years and a student of public policy, he occasionally has interesting things to say about software, technology, culture and politics.

Subscribe to feed Subscribe to my RSS Feed

  • View Aaron Longwell's profile on LinkedIn
  • Recommend Me