Created
May 19, 2021 15:29
-
-
Save roshanadh/3dd51453124f6114cada5a567ff6b380 to your computer and use it in GitHub Desktop.
Convert Java's LocalDateTime type to SQL TimeStamp type and vice-versa.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package np.com.roshanadhikary.mdblog.util; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
import java.sql.Timestamp; | |
import java.time.LocalDateTime; | |
@Converter(autoApply = true) | |
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> { | |
@Override | |
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) { | |
return localDateTime == null ? null : Timestamp.valueOf(localDateTime); | |
} | |
@Override | |
public LocalDateTime convertToEntityAttribute(Timestamp timestamp) { | |
return timestamp == null ? null : timestamp.toLocalDateTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment