Skip to content

Instantly share code, notes, and snippets.

@roshanadh
Created May 19, 2021 15:29
Show Gist options
  • Save roshanadh/3dd51453124f6114cada5a567ff6b380 to your computer and use it in GitHub Desktop.
Save roshanadh/3dd51453124f6114cada5a567ff6b380 to your computer and use it in GitHub Desktop.
Convert Java's LocalDateTime type to SQL TimeStamp type and vice-versa.
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