@DateTimeFormatter 没有前导零只是 yyyy-MM-dd 作为我的 SQL Spring 启动

@DateTimeFormatter without leading zero's just yyyy-MM-dd as My SQL Spring Boot

我正在 spring 引导应用程序中使用 sql 数据库。 1)sql 输入 DATE 和格式 yyyy-MM-DD。

2)当我使用Temporal类型时,它以正确的格式提取数据,但是在进行编辑和添加操作时,它似乎不允许。

3) 当我使用 DateTime 格式化程序时,所有操作都正常,但数据被拉起,或者在应用程序中添加了前导零,我不希望我的应用程序这样做。 Employee.java

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name="birth_date")
    @NotNull @Past
//  @Temporal(TemporalType.DATE)
    private Date birthDate;



    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name="hire_date")
    @NotNull
//  @Temporal(TemporalType.DATE)
    private Date hireDate;

请告知可以做什么,并在我将类型更改为 LocalDate 时添加,它会抛出

There was an unexpected error (type=Internal Server Error, status=500).
could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize

添加 Joda 依赖项会起作用,因为它支持语言环境日期转换和映射,根据 sql 是合适的,并且将被映射为日期(在 MySQL 上),即只有日期没有 timestamp/leading 零只是 YYYY-MM-DD。 在 pom.xml

中添加这些
      <dependency>
   <groupId>joda-time</groupId>
   <artifactId>joda-time</artifactId>
</dependency>

<dependency>
  <groupId>org.jadira.usertype</groupId>
  <artifactId>usertype.core</artifactId>
  <version>3.1.0.CR1</version>
 </dependency>

<dependency>
   <groupId>joda-time</groupId>
   <artifactId>joda-time-hibernate</artifactId>
    <version>1.3</version>
</dependency>
<dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time-jsptags</artifactId>
       <version>1.1.1</version>
 </dependency>

并将此发送给您的实体 class。

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name="birth_date")
@NotNull
private LocalDate birthDate;`   

public LocalDate getBirthDate() {
    return this.birthDate;
}

public void setBirthDate(LocalDate birthDate) {
    this.birthDate = birthDate;
}`