From 9b550e6554f0432f9e93a1f20273398909f17369 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 10 Jan 2019 15:00:38 +0400 Subject: [PATCH 1/2] localdate converter --- .../com/baeldung/util/LocalDateConverter.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java new file mode 100644 index 0000000000..341492d1fd --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java @@ -0,0 +1,25 @@ +package com.baeldung.util; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; +import java.sql.Date; +import java.time.LocalDate; +import java.util.Optional; + +@Converter(autoApply = true) +public class LocalDateConverter implements AttributeConverter { + + @Override + public Date convertToDatabaseColumn(LocalDate localDateTime) { + return Optional.ofNullable(localDateTime) + .map(Date::valueOf) + .orElse(null); + } + + @Override + public LocalDate convertToEntityAttribute(Date timestamp) { + return Optional.ofNullable(timestamp) + .map(Date::toLocalDate) + .orElse(null); + } +} \ No newline at end of file From 6202b7caab30ba5db67e992925efa60c21c2a881 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 10 Jan 2019 15:18:21 +0400 Subject: [PATCH 2/2] localdate converter --- .../main/java/com/baeldung/util/LocalDateConverter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java index 341492d1fd..00fd378b05 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/util/LocalDateConverter.java @@ -10,15 +10,15 @@ import java.util.Optional; public class LocalDateConverter implements AttributeConverter { @Override - public Date convertToDatabaseColumn(LocalDate localDateTime) { - return Optional.ofNullable(localDateTime) + public Date convertToDatabaseColumn(LocalDate localDate) { + return Optional.ofNullable(localDate) .map(Date::valueOf) .orElse(null); } @Override - public LocalDate convertToEntityAttribute(Date timestamp) { - return Optional.ofNullable(timestamp) + public LocalDate convertToEntityAttribute(Date date) { + return Optional.ofNullable(date) .map(Date::toLocalDate) .orElse(null); }