Thursday, May 25, 2023

What is difference between java.sql.Time, java.sql.Timestamp and java.sql.Date - JDBC interview Question

Difference between java.sql.Time, java.sql.Timestamp and java.sql.Date  is the most common JDBC question appearing on many core Java interviews. As JDBC provides three classes java.sql.Date, java.sql.Time and java.sql.Timestamp to represent date and time and you already have java.util.Date which can represent both date and time, this question poses a lot of confusion among Java programmer and that’s why this is one of those tricky Java questions are tough to answer. It becomes really tough if the differences between them are not understood correctly.

We have already seen some frequently asked or common JDBC questions like why JDBC has java.sql.Date despite java.util.Date and Why use PreparedStatement in Java in our last tutorials and we will see the difference between java.sql.Date, java.sql.Time and java.sql.Timestamp in this article.

By the way apart from these JDBC interview questions, if you are looking to get most from JDBC you can also see 4 JDBC performance tips and 10 JDBC best practices to follow. Those articles not only help you to understand and use JDBC better but also help with interviews. Let’s come back to different SQL time, timestamp, and SQL date.


Difference between java.sql.Time, java.sql.Timestamp and java.sql.Date:

JDBC in Java has three dates/time types corresponding to DATE, TIME and TIMESTAMP type of ANSI SQL. These types are used to convert SQL types into Java types.




1) First difference on java.sql.Time vs java.sql.Timestamp vs java.sql.Date is about information they represent :

JDBC TIME or java.sql.Time represents only time information e.g. hours, minutes, and seconds without any date information.

JDBC DATE or java.sql.Date represents only date information e.g. year, month, and day without any time information.

JDBC TIMESTAMP or java.sql.Timestamp represents both date and time information including nanosecond details.


2) java.sql.Time and java.sql.Timestamp extends java.util.Date class but java.sql.Date is independent.

3) Time information from java.sql.Date and Date information from java.sql.Time is normalized and may be set to zero in order to confirm ANSI SQL DATE and TIME types.

And, if you like to see difference in tabular format, here is a nice table which highlights the difference between Time, Date, and Timestamp in Java SQL API

What is difference between java.sql.Time, java.sql.Timestamp and java.sql.Date - JDBC interview Question





difference between java.sql.Date , java.sql.Time and java.sql.Timestamp in JDBCSo the difference between Time, Timestamp, and Date of SQL package is clear in terms of what they represent. On the contrary java.util.Date also represents Date and time information but without nanosecond details and that's why many people prefer to store date as a long value (millisecond passed from epoch January 1, 1970 00:00:00.000 GMT). If you compare to java.sql.Timestamp with equals() the method it will return false as value of nanosecond is unknown.

That's all on the difference between java.sql.Date, java.sql.Time and java.sql.Timestamp. All differences lie in what exactly they represent. These kinds of questions are worth looking at before going to any JDBC interview as time and date are integral parts of any JDBC interview.


Other JDBC and SQL articles from Javarevisited Blog

2 comments :

Peddi said...

2) java.sql.Time and java.sql.Timestamp extends java.util.Date class but java.sql.Date is independent.

This point is INCORRECT.
All three types java.sql.Time, java.sql.Timestamp , and java.sql.Date are extended from from java.util.Date (http://docs.oracle.com/javase/7/docs/api/java/util/Date.html)

javin paul said...

Hello @Peddi, you are absolutely correct, all three date and time classes from java.sql package e.g. Time, Timestamp, and Date extends java.util.Date but more importantly they violate Liskov substitution principle, hence cannot be used in place of each other. For example, you cannot pass java.sql.Timestamp to a method expecting java.util.Date even though Timestamp contains both date and time part.

Post a Comment