20

I have hibernate entity and a bean:

@Entity
public class GeneralObservation {
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    Date date;

    @Column
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

also I have

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

and

        form:input
                id = "datepicker"
                name="date"
                itemLabel="date"
                path="newObservation.date"

When I go to my url I see:

my form

How can I force it to have mm/DD/yyyy format? Thanx

6
  • @DateTimeFormat(pattern = "dd/MM/yyyy") and new SimpleDateFormat("dd/MM/yyyy") both use dd/MM/yyyy. Have you tried to change them to mm/DD/yyyy? Commented Aug 10, 2013 at 15:34
  • 2
    please refer stackoverflow.com/questions/3457134/… Commented Aug 10, 2013 at 15:38
  • No, this wouldn't help, because form:input doesn't allow to put default value explicitly. Commented Aug 10, 2013 at 16:42
  • 1
    did you use mvc:annotation-driven? Commented Aug 19, 2013 at 13:12
  • no, where should I put mvc:annotation-driven? Commented Aug 21, 2013 at 16:08

4 Answers 4

23

You can use fmt:formatDate jstl tag:

<fmt:formatDate value="${yourObject.date}" var="dateString" pattern="dd/MM/yyyy" />
<form:input path="date" value="${dateString} .. />
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, sorry for misrepresentation. In my application I have same situation, but date format is correct (like I specify in DateTimeFormat annotation before field). And not only presentation of field is correct: I have no @InitBinder method and all works correct. Maybe I have newer version of spring library (3.1.1.RELEASE)?
8

The solution is to put
<mvc:annotation-driven/> to mvc-dispatcher-servlet.xml and also xmlns:mvc="http://www.springframework.org/schema/mvc" to the begining of xml file.

1 Comment

what if I don't use xml for dispatcher ?
5

in my code I use the binder in this way:

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

2 Comments

Try: $(document).ready(function(){ $(function() { $("#datepicker").datepicker({ changeMonth : true, changeYear : true, dateFormat : 'dd/MM/yyyy' }); }); <form:input path="datepicker" />
I don't need datepicker. Date format is not what I've expected.
-2

In HTML5 there is input type="date", same thing in Spring (Chrome , Opera, and I this Safary too, but not yet in Firefox

<form:input type="date" ... >

1 Comment

why is this in -ve ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.