Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -103,10 +105,10 @@ public String format(Date date) {
* @return the new created object
*/
public <T extends TemporalAccessor> T parse(String input, Class<T> type) {
TemporalAccessor accessor = dateFormatter.parse(input);
ZonedDateTime zonedDateTime = DateFormatters.from(dateFormatter.parse(input));
try {
Method method = type.getMethod("from", TemporalAccessor.class);
Object o = method.invoke(null, accessor);
Object o = method.invoke(null, zonedDateTime);
return type.cast(o);
} catch (NoSuchMethodException e) {
throw new ConversionException("no 'from' factory method found in class " + type.getName());
Expand All @@ -122,6 +124,7 @@ public <T extends TemporalAccessor> T parse(String input, Class<T> type) {
* @return the new created object
*/
public Date parse(String input) {
return new Date(Instant.from(dateFormatter.parse(input)).toEpochMilli());
ZonedDateTime zonedDateTime = DateFormatters.from(dateFormatter.parse(input));
return new Date(Instant.from(zonedDateTime).toEpochMilli());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
Expand Down Expand Up @@ -98,4 +99,36 @@ void shouldConvertInstantToString() {

assertThat(formatted).isEqualTo("1234568901234");
}

@Test // DATAES-953
@DisplayName("should write and read Date with custom format")
void shouldWriteAndReadDateWithCustomFormat() {

// only seconds as the format string does not store millis
long currentTimeSeconds = System.currentTimeMillis() / 1_000;
Date date = new Date(currentTimeSeconds * 1_000);

ElasticsearchDateConverter converter = ElasticsearchDateConverter.of("uuuu-MM-dd HH:mm:ss");

String formatted = converter.format(date);
Date parsed = converter.parse(formatted);

assertThat(parsed).isEqualTo(date);
}

@Test // DATAES-953
@DisplayName("should write and read Instant with custom format")
void shouldWriteAndReadInstantWithCustomFormat() {

// only seconds as the format string does not store millis
long currentTimeSeconds = System.currentTimeMillis() / 1_000;
Instant instant = Instant.ofEpochSecond(currentTimeSeconds);

ElasticsearchDateConverter converter = ElasticsearchDateConverter.of("uuuu-MM-dd HH:mm:ss");

String formatted = converter.format(instant);
Instant parsed = converter.parse(formatted, Instant.class);

assertThat(parsed).isEqualTo(instant);
}
}