Frage

I use the following jQuery function to format my datepicker

$(".datepicker").datepicker({
                    dateFormat : "MM yy"
                }).attr('readonly', true);

Upon selection I can see the text field is set correctly to November 2013. Before form is submitted I am using Spring validation to validate the date with

public class LocalMonthEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(final String text) throws IllegalArgumentException {
        if (!StringUtils.hasText(text)) {
            // Treat empty String as null value.
            setValue(null);
        } else {
            LocalDateTime localDateTime = LocalDateTime.parse(text,
                    DateTimeFormat.forPattern("MMMM yyyy"));
            setValue(localDateTime);
        }
    }

    @Override
    public void setValue(final Object value) {
        super.setValue(value == null || value instanceof LocalDateTime ? value
                : new LocalDateTime(value));
    }

    @Override
    public LocalDateTime getValue() {
        return (LocalDateTime) super.getValue();
    }

    @Override
    public String getAsText() {
        return getValue() != null ? getValue().toString() : "";

    }
}

However after form being submitted the text field is changed to 2013-11-01T00:00:00.000. How can I maintain the field to November 2013 ?

War es hilfreich?

Lösung

First of all, if the data you need is simply a month and a year, why are you using Joda-Time at all? That's like getting in your car to drive to your mailbox at the end of the driveway: extra effort and complexity for no benefit.

Instead, I suggest you choose between:

  • Track a pair of variables (month, year)
  • Define your own class with a pair of members (month, year), and track an instance.
  • Use a String as seems to be your intention: "November 2013" as you seem to be thinking, or a simpler schemes such as "2013-11".

Secondly, because you created an instance of LocalDatetime, at some point toString seems to be called. The default output of toString on a LocalDateTime is output in the standard ISO 8601 format you saw: 2013-11-01T00:00:00.000. A LocalDateTime has a date value and a time value (hence the name), even if the time value may be set to zeros (meaning start of day). So this is a feature, not a bug.

I don't know Spring Validatation nor the rest of your class structure. I'm guessing you are storing a LocalDateTime instance where instead you meant to be (or should be) storing a String instance. You may need to read up on the subject of "model" versus "view". Often we track data behind the scenes differently than we present data to the user. In this case, you probably should be holding a pair of ints or Integers (one for month, one for year) in your model with a String in your view ("November 2013").

Andere Tipps

You are setting your local time object directly to text field, thats why your getting full date string.convert your date object by using parse() method and set it. Do not create new object for date set your value directly.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top