ciurlaro
ciurlaro

Reputation: 1024

Failed to convert 1985-04-12T23:20 into java.util.Date

[Spring + Kotlin]

These are the dependencies:

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-data-rest")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-web-services")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

This is the Entity:

@Entity
class MatchEntity(
        @Id @GeneratedValue val id: Long,
        @NotBlank val matchDateTime: Date,
        @NotBlank @ManyToOne @JoinColumn val tournamentInvolved: TournamentEntity
)

Whenever I try to run the following query:

interface MatchRepository : JpaRepository<MatchEntity, Long> {
    fun findMatchesByMatchDateTimeIsAfter(matchDateTime: Date)
}

with a test string like so 1985-04-12T23:20, I get the error:

QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.util.Date!

I tried, as suggested here, with patterns like @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) and @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") in the signature of the query method, without solving.

Also, as suggested here, I tried adding

  1. compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0") to the dependencies

  2. spring.jackson.serialization.write_dates_as_timestamps=false to the application.properties.

Didn't work.

UPDATE: I also tried with LocalDateTime and Instant classes. Still getting the same Exceptions:

QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.time.LocalDateTime!

QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.time.Instant!

Upvotes: 2

Views: 730

Answers (1)

ciurlaro
ciurlaro

Reputation: 1024

Solved

Using @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") worked.

Upvotes: 3

Related Questions