Partner – DBSchema – NPI (tag = SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

>> Take a look at DBSchema

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.