Making a getAsText() with Date in PropertyEditorSupport and @Initbinder

189 Views Asked by At

I need to convert date with pattern "yyyy-MM-dd" to String format "dd.MM.yyyy". Now i have this. Method getAsText() doesn't work meanwhile setAsText working. What i do wrong?

DateEditor.java

@Component
public class DateEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String value) {
        try {
            setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
        } catch (Exception ex) {
            setValue(null);
        }
    }

     @Override
    public String getAsText() {

        String sdf = "";

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            dateFormat.setLenient(true);//-

            Date date = dateFormat.parse((String) getValue());
            sdf = new SimpleDateFormat("dd.MM.yyyy").format(date);

            System.out.println(sdf);
        } catch (ParseException p) {}

        return sdf;
    }

}

Initbinder

@InitBinder
    public void initBinder(WebDataBinder dataBinder) {
         dataBinder.registerCustomEditor(Date.class,new DateEditor());
}
0

There are 0 best solutions below