Skip to content

Commit 13ab8a1

Browse files
authored
Merge pull request eugenp#6120 from MherBaghinyan/BAEL-2521
Bael 2521
2 parents e741218 + 6202b7c commit 13ab8a1

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.util;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
import java.sql.Date;
6+
import java.time.LocalDate;
7+
import java.util.Optional;
8+
9+
@Converter(autoApply = true)
10+
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
11+
12+
@Override
13+
public Date convertToDatabaseColumn(LocalDate localDate) {
14+
return Optional.ofNullable(localDate)
15+
.map(Date::valueOf)
16+
.orElse(null);
17+
}
18+
19+
@Override
20+
public LocalDate convertToEntityAttribute(Date date) {
21+
return Optional.ofNullable(date)
22+
.map(Date::toLocalDate)
23+
.orElse(null);
24+
}
25+
}

0 commit comments

Comments
 (0)