Working With SQL Dates in Java : Mini Tutorial For Beginners

Dhananjay Trivedi
Sep 4, 2018 · 2 min read

In this short tutorial, I will show you very quickly how easy is to work with java.sql.Date for your JDBC application. I have seen a lot of people getting confused on how to parse dates and store it in Database through your Java application (I was no different BTW).

Let’s jump into it.

To clear things out, keep these things in mind.

1. As per Java documentation java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.

2. Java.sql.Date just represents DATE without time information while java.util.Date represents both Date and Time information. This is the major differences why java.util.Date can not directly map to java.sql.Date. Upcasting happens implicitly so need not worry about that.

Although, many of the methods of java.util.Date has been deprecated so don’t be too dependent on it while working with Dates. LocalDate class in Java 8 is a much more Thread safe, dependable option. I will be adding codes working with LocalDate very soon.

Scenario 1 : Getting date from user as input and then storing that in Database.

Scenario 2 : Getting SQL Date From Database and then displaying it to user / Formatting the SQL Date into other formats

The output for the Scenario 2 :

(Running the above code on Sept 4 2018)

Default Format Of Our SQL date is : 2018-09-04
Util Date : 2018-09-04
New Format : 04-09-2018
SQL Date As String : 2018-09-04
New Formatted Util Date : Tue Sep 04 00:00:00 IST 2018

Hope this help clearing things out for you. Feel free to reach out to me if you have any doubts / suggestions for improvement.

Keep Learning!

DJ out!

Dhananjay Trivedi

Written by

Developer and an accidental Writer. Learner & an accidental Teacher. Full Stack Android Engineer @ Softway.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade