1. Overview
In this quick tutorial, we’ll learn how to convert between java.time.LocalDate and java.sql.Date.
2. Direct Conversion
To convert from LocalDate to java.sql.Date,we can simply use the valueOf() method available in java.sql.Date. Likewise, to convert the current date, we can use:
Date date = Date.valueOf(LocalDate.now());
Or, any other specific date:
Date date = Date.valueOf(LocalDate.of(2019, 01, 10));
Moreover, valueOf() throws NullPointerException in case of a null argument.
Now, let’s convert from java.sql.Date to LocalDate. For that, we can use the toLocalDate() method:
LocalDate localDate = Date.valueOf("2019-01-10").toLocalDate();
3. Using an AttributeConverter
First, let’s understand the problem.
Java 8 has lots of useful features, including the Date/Time API.
However, using it with some databases or persistence frameworks requires a bit more work than expected. For instance, JPA will map the LocalDate property into a blob instead of the java.sql.Date object. As a result, the database won’t recognize the LocalDate property as a Date type.
In general, we don’t want to perform an explicit conversion between the LocalDate and Date.
For example, suppose we have an entity object with a LocalDate field. When persisting this entity, we need to tell the persistence context how to map the LocalDate into the java.sql.Date.
Let’s apply a simple solution by creating an AttributeConverter class:
@Converter(autoApply = true) public class LocalDateConverter implements AttributeConverter<LocalDate, Date> { @Override public Date convertToDatabaseColumn(LocalDate localDate) { return Optional.ofNullable(localDate) .map(Date::valueOf) .orElse(null); } @Override public LocalDate convertToEntityAttribute(Date date) { return Optional.ofNullable(date) .map(Date::toLocalDate) .orElse(null); } }
As we can see, the AttributeConverter interface accepts two types: LocalDate and Date in our case.
In short, the convertToDatabaseColumn() and convertToEntityAttribute() methods will take care of the conversion process. Inside of the implementations, we use Optional to easily handle possible null references.
Moreover, we’re also using the @Converter annotation. With the autoApply=true property, the converter will be applied to all mapped attributes of the entity’s type.
4. Conclusion
In this quick tutorial, we showed two ways to convert between java.time.LocalDate and java.sql.Date. Moreover, we presented examples using direct conversion and using a custom AttributeConverter class.
As usual, the complete code for this article is available over on GitHub.