如何将模型中的两个日期字段绑定到Spring中的不同日期格式?

时间:2013-07-10 20:47:32

标签: spring date data-binding date-format

我正在使用Spring 3.1.1.RELEASE。我有一个带有两个java.util.Date字段的模型......

public class MyModel
{
    ...
        private java.util.Date startDate;
        private java.util.Date meetingTime;

如何设置活页夹,以便根据不同的日期格式转换每个字段?目前我有

@InitBinder
public void initBinder(WebDataBinder binder) {
    final DateFormat dateFormat = new SimpleDateFormat(dateFormat1);
    dateFormat.setLenient(false);

但这不起作用,因为我希望使用不同的格式转换每个字段。谢谢, - 戴夫

1 个答案:

答案 0 :(得分:0)

您可以使用@DateTimeFormat

代替init binder
@DateTimeFormat(pattern = "dd/MM/yyyy")
private java.util.Date startDate;

@DateTimeFormat(pattern = "dd MMM yyyy")
private java.util.Date meetingTime;

如果您使用的是早于3.2的Spring,则可能需要在classpath上包含joda-time