Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Joda LocalDate的属性编辑器异常_Java_Spring_Spring Mvc_Editor_Jodatime - Fatal编程技术网

Java Joda LocalDate的属性编辑器异常

Java Joda LocalDate的属性编辑器异常,java,spring,spring-mvc,editor,jodatime,Java,Spring,Spring Mvc,Editor,Jodatime,我用的是乔达和本地约会。我创建了一个自定义属性编辑器,它从视图中接收正确的值,如“23-05-2017”,但当我尝试解析它时,我得到: LocalDatePropertyEditor - Error Conversione DateTime java.lang.IllegalArgumentException: Invalid format: "23-05-2017" is malformed at "-05-2017" 这是我的自定义编辑器: public class LocalDatePr

我用的是乔达和本地约会。我创建了一个自定义属性编辑器,它从视图中接收正确的值,如
“23-05-2017”
,但当我尝试解析它时,我得到:

LocalDatePropertyEditor - Error Conversione DateTime
java.lang.IllegalArgumentException: Invalid format: "23-05-2017" is malformed at "-05-2017"
这是我的自定义编辑器:

public class LocalDatePropertyEditor extends PropertyEditorSupport{
    private final DateTimeFormatter formatter;

    final Logger logger = LoggerFactory.getLogger(LocalDatePropertyEditor.class);   

    public LocalDatePropertyEditor(Locale locale, MessageSource messageSource) {
        this.formatter = DateTimeFormat.forPattern( messageSource.getMessage("dateTime_pattern", new Object[]{}, locale));
    }

    public String getAsText() {
        LocalDate value = ( LocalDate ) getValue();
        return value != null ? new LocalDate( value ).toString( formatter ) : "";
    }

    public void setAsText( String text ) throws IllegalArgumentException {
        LocalDate val;
        if (!text.isEmpty()){
            try{
                val = DateTimeFormat.forPattern("dd/MM/yyyy").parseLocalDate(text);

                setValue(val);
            }catch(Exception e){

                logger.error("Errore Conversione DateTime",e);
                setValue(null);
            }
        }else{
            setValue(null);
        }
    }
}
在控制器内部,我注册了它:

@InitBinder
    protected void initBinder(final ServletRequestDataBinder binder, final Locale locale) {
        binder.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor(locale, messageSource));
    }

如何修复此错误?

问题在于您用于解析
LocalDate
的模式

而不是:

val = DateTimeFormat.forPattern("dd/MM/yyyy").parseLocalDate(text);
使用以下命令:

val = DateTimeFormat.forPattern("dd-MM-yyyy").parseLocalDate(text);

如果您的日期格式为2017年5月23日,则您使用了错误的模式。您应该使用
dd-MM-yyyy
而不是
dd/MM/yyyy

我测试了它,只需在控制器中使用以下命令, 如果愿意,您可以更改模式“dd-MM-yyyy”

@InitBinder
private void dateBinder(WebDataBinder binder) {

    PropertyEditor editor = new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!text.trim().isEmpty())
                super.setValue(LocalDate.parse(text.trim(), DateTimeFormatter.ofPattern("dd-MM-yyyy")));
        }
        @Override
        public String getAsText() {
            if (super.getValue() == null)
                return null;
            LocalDate value = (LocalDate) super.getValue();
            return value.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        }
    };
    binder.registerCustomEditor(LocalDate.class, editor);
}