Convert between LocalDate to java.sql.Date

Java 8 examples to convert from LocalDate to java.sql.Date and vice versa.

1. Convert java.sql.Date to LocalDate

Use java.sql.Date.toLocalDate() method to get LocalDate from a java.sql.Date.

//Get SQL date instance
java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());

//Get LocalDate from SQL date
LocalDate localDate = sqlDate.toLocalDate();

System.out.println( localDate );    //2022-01-15

2. Convert LocalDate to java.sql.Date

Use java.sql.Date.valueOf() method to get SQL date from a LocalDate.

//Local date instance
LocalDate localDate = LocalDate.now();

//Get LocalDate from SQL date
java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );

System.out.println( sqlDate );  //2022-01-15    

Happy Learning !!

Sourcecode on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.