localdate converter

This commit is contained in:
mherbaghinyan 2019-01-10 15:00:38 +04:00
parent 68f1cb23dd
commit 9b550e6554
1 changed files with 25 additions and 0 deletions

View File

@ -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<LocalDate, Date> {
@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);
}
}