Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 为什么使用DateTimeFormat无法转换属性错误_Java_Spring_Spring Boot - Fatal编程技术网

Java 为什么使用DateTimeFormat无法转换属性错误

Java 为什么使用DateTimeFormat无法转换属性错误,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试以dd/MM/yyyy格式绑定日期,我尝试了许多方法,如: 初始粘结剂 @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); dateFormat.setLenie

我正在尝试以dd/MM/yyyy格式绑定日期,我尝试了许多方法,如:

初始粘结剂

@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null,  new CustomDateEditor(dateFormat, true));
    }
日期时间格式注释

@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)
private Date fecha;
配置类

@Configuration
public class DateTimeConfig {

    @Bean
    public FormattingConversionService conversionService() {
        DefaultFormattingConversionService conversionService = 
          new DefaultFormattingConversionService(false);

        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setDateFormatter(DateTimeFormatter.ofPattern("dd-MM-yyyy"));        
        registrar.registerFormatters(conversionService);

        // other desired formatters

        return conversionService;
    }
}
但以上都不起作用,我总是会犯这样的错误:

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'fecha'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat @javax.persistence.Temporal java.util.Date] for value '2018-10-09'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2018-10-09" is malformed at "18-10-09"]
实际上,我的模型是:

@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)
private Date fecha;

<input name="fecha" type="date" th:value="*{fecha}" class="form-control" />
@DateTimeFormat(pattern=“dd/MM/yyyy”)
@时态(TemporalType.DATE)
私人约会费恰;

问题在于Spring只接受@DateTimeFormat标记中的yyyy-MM-dd格式日期,您应该使用新的Java时间类LocalDate

实体

@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate fecha;
百里香

<input name="fecha" type="date" th:value="*{fecha}" class="form-control" />


输入可以以任何格式显示,在我的例子中,我以dd/MM/yyyy格式显示日期,然后Spring自动转换为yyyy-MM-dd

输入以ISO格式提供日期。例如2019-03-21。这是用于将其序列化为正确日期的格式。以后你可以用另一种方式格式化日期。你能解释得更清楚些吗?我知道最初的格式是2019-03-21,但是我如何格式化它呢?看起来你把几年前被取代的糟糕的日期类和它们的替代品java.time类混在了一起。仅使用现代类,如
LocalDate
DateTimeFormatter
。永远不要使用
Date
SimpleDateFormat
。我不明白这是怎么一个问题?如果你知道你得到了一个字符串日期,比如“2019-03-21”,为什么不使用“yyyy-MM-dd”模式作为你的格式化程序呢?或者更好的是
DateTimeFormatter.ISO\u LOCAL\u DATE
@juliansolate您想更改输入中日期的格式吗?如果是,则无法执行此操作,因为它取决于浏览器<代码>是HTML5规范的一部分,非常新鲜。相反,您应该放弃
type=date
,转而选择带有日期选择js插件/组件的
type=text
。这样地: