It is a composite kind of java.util.Date and an extra nanosecond worth which is fitted there to substantiate database DATETIME information kind, which helps nanosecond precision. If you happen to take a look at the implementation of java.sql.Timestamp class, you will see that the lengthy worth provided by Date is saved individually than this nanosecond worth.
You can’t even evaluate Date to Timestamp class in Java. The Timestamp.equals(Object) technique returns false whenever you cross an occasion of java.util.Date as a result of the nanos part of a date is unknown.
In consequence, the Timestamp.equals(Object) technique is just not symmetric with respect to the java.util.Date.equals(Object) technique.
Additionally, the hashCode technique makes use of the underlying java.util.Date implementation and subsequently doesn’t embody nanos in its computation.
And, if you’re new to JDBC and on the lookout for a complete on-line course to study JDBC in-depth then I additionally recommend you take a look at these greatest JDBC coursess on Udemy. It is a terrific course of direct classroom lectures and covers JDBC in depth
To know the distinction between Timestamp and Date higher, let me present an instance of evaluating Date and Timestamp objects in Java:
import java.sql.Timestamp; import java.util.Date; /* * Java Program to check Date and Timestamp in JDBC */ public class TimeStampVsDate { public static void most important(String[] args) { Date date = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); if (date.equals(timestamp)) { System.out .println("Each Date and Timestamp are equal, whenever you evaluate utilizing Date.equals() technique"); } if (!timestamp.equals(date)) { System.out .println("However, they aren't equal whenever you evaluate them utilizing Timestamp.equals() technique"); } } } Output Each Date and Timestamp are equal, whenever you evaluate utilizing Date.equals() technique However, they're not equal whenever you evaluate them utilizing Timestamp.equals() technique
So, you’ll be able to see they break the symmetry property of equals() technique which says if a.equals(b) is true then b.equals(a) must also be true.
For many who want a bit little bit of revision about which Java kind mapped to which SQL kind within the database, here’s a good slide about Java to SQL kind mapping:
So, although each Date and Timestamp incorporates date and time worth and their respective getMonth(), getHours() technique would not throw IllegalArgumentException, you shouldn’t be utilizing Timestamp rather than java.util.Date as a result of variations talked about above.