Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring 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> {

    public Date convertToDatabaseColumn(LocalDate localDate) {
        return Optional.ofNullable(localDate)

    public LocalDate convertToEntityAttribute(Date date) {
        return Optional.ofNullable(date)

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.

Java bottom

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

Generic footer banner
Comments are closed on this article!