Reputation: 73
I'm using spring-data-elasticsearch 4.0.1 and elastic cluster 7.6, when I define an attribute with a custom pattern with "yyyy-MM-dd" and try to retrieve a date with value "2014-06-11" it throws an error.
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd")
private Date startDate;
Error:
java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2014-06-11 of type java.time.format.Parsed
I tried with this also but get an error again:
@Field(type = FieldType.Date, format = DateFormat.date_optional_time)
private Date startDate;
I read in the documentation that I should use the pattern "uuuu-MM-dd" for elastic 7 version, but this doesn't work either.
Upvotes: 1
Views: 2158
Reputation: 1
This issue has been resolved By Spring Data Elasticsearch 4.0.5. Spring Boot 2.3.5 use Spring Data Elasticsearch 4.0.5. (Spring Boot 2.3.5 available Oct 29.2020.)
https://github.com/spring-projects/spring-data-elasticsearch/pull/538 DATAES-953 - DateTimeException on converting Instant or Date to custo…
https://github.com/spring-projects/spring-data-elasticsearch/releases/tag/4.0.5.RELEASE
Upvotes: 0
Reputation: 79005
Elasticsearch 7 uses the modern date-time API. Given below is an excerpt from https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference
Properties that derive from TemporalAccessor must either have a @Field annotation of type FieldType.Date or a custom converter must be registered for this type. If you are using a custom date format, you need to use uuuu for the year instead of yyyy. This is due to a change in Elasticsearch 7.
Change you annotation and type as follows:
@Field(type = FieldType.Date, format = DateFormat.date)
private LocalDate startDate;
Learn more about the modern date-time API at Trail: Date Time.
Some more references:
Upvotes: 1
Reputation: 19421
A java.util.Date
is not a plain date consisting of a year,month and day, but an instant in time in the UTC timezone. There is no way to convert "2014-06-11" to an instant in time. What hours and minutes should be used? In what timezone?
Like Ole wrote in the comment, use java.time.LocalDate
for this. This class is for exactly that use case: a year with a month and a day. And please stop using the old java.util.Date
class. Since Java 8, there are the classes in the java.time
package.
Upvotes: 1