DATAMONGO-1102 - Added support for Java 8 date/time types.
We're now able to persist and read non-time-zoned JDK 8 date/time types (LocalDate, LocalTime, LocalDateTime) to and from Date instances.
This commit is contained in:
@@ -36,6 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter;
|
||||
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.data.convert.JodaTimeConverters;
|
||||
import org.springframework.data.convert.Jsr310Converters;
|
||||
import org.springframework.data.convert.ReadingConverter;
|
||||
import org.springframework.data.convert.WritingConverter;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
@@ -112,6 +113,7 @@ public class CustomConversions {
|
||||
|
||||
toRegister.addAll(JodaTimeConverters.getConvertersToRegister());
|
||||
toRegister.addAll(GeoConverters.getConvertersToRegister());
|
||||
toRegister.addAll(Jsr310Converters.getConvertersToRegister());
|
||||
|
||||
for (Object c : toRegister) {
|
||||
registerConversion(c);
|
||||
|
||||
@@ -23,6 +23,7 @@ import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -1945,6 +1946,22 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(withAnnotatedIdField.key, is("A"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-1102
|
||||
*/
|
||||
@Test
|
||||
public void convertsJava8DateTimeTypesToDateAndBack() {
|
||||
|
||||
TypeWithLocalDateTime source = new TypeWithLocalDateTime();
|
||||
LocalDateTime reference = source.date;
|
||||
BasicDBObject result = new BasicDBObject();
|
||||
|
||||
converter.write(source, result);
|
||||
|
||||
assertThat(result.get("date"), is(instanceOf(Date.class)));
|
||||
assertThat(converter.read(TypeWithLocalDateTime.class, result).date, is(reference));
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -2232,4 +2249,13 @@ public class MappingMongoConverterUnitTests {
|
||||
|
||||
@Id String key;
|
||||
}
|
||||
|
||||
static class TypeWithLocalDateTime {
|
||||
|
||||
LocalDateTime date;
|
||||
|
||||
TypeWithLocalDateTime() {
|
||||
this.date = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user