diff --git a/pom.xml b/pom.xml
index cb2a347c..47999643 100644
--- a/pom.xml
+++ b/pom.xml
@@ -87,6 +87,13 @@
${jackson}
+
+ joda-time
+ joda-time
+ ${jodatime}
+ true
+
+
org.hamcrest
hamcrest-all
diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java b/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java
index 23d603cf..435534d0 100644
--- a/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java
+++ b/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java
@@ -16,9 +16,14 @@
package org.springframework.data.couchbase.core.convert;
+import org.joda.time.DateMidnight;
+import org.joda.time.DateTime;
+import org.joda.time.LocalDate;
+import org.joda.time.LocalDateTime;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
+import org.springframework.util.ClassUtils;
import java.util.ArrayList;
import java.util.Calendar;
@@ -35,13 +40,32 @@ public final class DateConverters {
private DateConverters() {}
+ private static final boolean JODA_TIME_IS_PRESENT = ClassUtils.isPresent("org.joda.time.LocalDate", null);
+
+ /**
+ * Returns all converters by this class that can be registered.
+ *
+ * @return the list of converters to register.
+ */
public static Collection> getConvertersToRegister() {
List> converters = new ArrayList>();
+
converters.add(DateToLongConverter.INSTANCE);
converters.add(CalendarToLongConverter.INSTANCE);
-
converters.add(LongToDateConverter.INSTANCE);
converters.add(LongToCalendarConverter.INSTANCE);
+
+ if (JODA_TIME_IS_PRESENT) {
+ converters.add(LocalDateToLongConverter.INSTANCE);
+ converters.add(LocalDateTimeToLongConverter.INSTANCE);
+ converters.add(DateTimeToLongConverter.INSTANCE);
+ converters.add(DateMidnightToLongConverter.INSTANCE);
+ converters.add(LongToLocalDateConverter.INSTANCE);
+ converters.add(LongToLocalDateTimeConverter.INSTANCE);
+ converters.add(LongToDateTimeConverter.INSTANCE);
+ converters.add(LongToDateMidnightConverter.INSTANCE);
+ }
+
return converters;
}
@@ -51,7 +75,7 @@ public final class DateConverters {
@Override
public Long convert(Date source) {
- return source.getTime();
+ return source == null ? null : source.getTime();
}
}
@@ -61,7 +85,7 @@ public final class DateConverters {
@Override
public Long convert(Calendar source) {
- return source.getTimeInMillis() / 1000;
+ return source == null ? null : source.getTimeInMillis() / 1000;
}
}
@@ -71,6 +95,10 @@ public final class DateConverters {
@Override
public Date convert(Long source) {
+ if (source == null) {
+ return null;
+ }
+
Date date = new Date();
date.setTime(source);
return date;
@@ -83,10 +111,94 @@ public final class DateConverters {
@Override
public Calendar convert(Long source) {
+ if (source == null) {
+ return null;
+ }
+
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(source * 1000);
return calendar;
}
}
+ @WritingConverter
+ public enum LocalDateToLongConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public Long convert(LocalDate source) {
+ return source == null ? null : source.toDate().getTime();
+ }
+ }
+
+ @WritingConverter
+ public enum LocalDateTimeToLongConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public Long convert(LocalDateTime source) {
+ return source == null ? null : source.toDate().getTime();
+ }
+ }
+
+ @WritingConverter
+ public enum DateTimeToLongConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public Long convert(DateTime source) {
+ return source == null ? null : source.toDate().getTime();
+ }
+ }
+
+ @WritingConverter
+ public enum DateMidnightToLongConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public Long convert(DateMidnight source) {
+ return source == null ? null : source.toDate().getTime();
+ }
+ }
+
+ @ReadingConverter
+ public enum LongToLocalDateConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public LocalDate convert(Long source) {
+ return source == null ? null : new LocalDate(source);
+ }
+ }
+
+ @ReadingConverter
+ public enum LongToLocalDateTimeConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public LocalDateTime convert(Long source) {
+ return source == null ? null : new LocalDateTime(source);
+ }
+ }
+
+ @ReadingConverter
+ public enum LongToDateTimeConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public DateTime convert(Long source) {
+ return source == null ? null : new DateTime(source);
+ }
+ }
+
+ @ReadingConverter
+ public enum LongToDateMidnightConverter implements Converter {
+ INSTANCE;
+
+ @Override
+ public DateMidnight convert(Long source) {
+ return source == null ? null : new DateMidnight(source);
+ }
+ }
+
}
diff --git a/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java b/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java
index 69d32d68..80a451a3 100644
--- a/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java
+++ b/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java
@@ -16,6 +16,7 @@
package org.springframework.data.couchbase.core.mapping;
+import org.joda.time.LocalDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -387,16 +388,19 @@ public class MappingCouchbaseConverterTests {
public void writesAndReadsDates() {
Date created = new Date();
Calendar modified = Calendar.getInstance();
- DateEntity entity = new DateEntity(created, modified);
+ LocalDateTime deleted = LocalDateTime.now();
+ DateEntity entity = new DateEntity(created, modified, deleted);
CouchbaseDocument converted = new CouchbaseDocument();
converter.write(entity, converted);
assertEquals(created.getTime(), converted.getPayload().get("created"));
assertEquals(modified.getTimeInMillis() / 1000, converted.getPayload().get("modified"));
+ assertEquals(deleted.toDate().getTime(), converted.getPayload().get("deleted"));
DateEntity read = converter.read(DateEntity.class, converted);
assertEquals(created.getTime(), read.created.getTime());
assertEquals(modified.getTimeInMillis() / 1000, read.modified.getTimeInMillis() / 1000);
+ assertEquals(deleted.toDate().getTime(), read.deleted.toDate().getTime());
}
static class EntityWithoutID {
@@ -507,9 +511,12 @@ public class MappingCouchbaseConverterTests {
static class DateEntity extends BaseEntity {
private Date created;
private Calendar modified;
- public DateEntity(Date created, Calendar modified) {
+ private LocalDateTime deleted;
+
+ public DateEntity(Date created, Calendar modified, LocalDateTime deleted) {
this.created = created;
this.modified = modified;
+ this.deleted = deleted;
}
}