Why Timestamp Cannot Endure Used Inward House Of Appointment Inward Java?
One of the tricky interrogation from Java Interview is, "Can nosotros move past times a Timestamp event to a method expecting java.util.Date?", it's a tricky interrogation because the reply is both Yes and No. You can, of course, move past times a Timestamp object to a method alongside the declaration every bit Date because first, Timestamp is a subclass of java.util.Date in addition to instant it contains both engagement in addition to fourth dimension values which are missing inward either java.sql.Date in addition to java.sql.Time. So at that topographic point is to a greater extent than argue to move past times a Timestamp value to Date just you lot should non hold upwards doing that. Why? because Timestamp is non just Date. It's a composite type of java.util.Date in addition to an additional nanosecond value which is fitted at that topographic point to confirm database DATETIME information type, which supports nanosecond precision. If you lot facial expression at the implementation of java.sql.Timestamp class, you lot volition detect that the long value supplied past times Date is stored separately in addition to then this nanosecond value.
You cannot fifty-fifty compare Date to Timestamp class inward Java. The Timestamp.equals(Object) method returns simulated when you lot move past times an event of java.util.Date because the nanos constituent of a engagement is unknown. As a result, the Timestamp.equals(Object) method is non symmetric alongside abide by to the java.util.Date.equals(Object) method. Also, the hashCode method uses the underlying java.util.Date implementation in addition to thence does non include nanos inward its computation. See Core Java Volume two - Advanced Features past times Cay S. Horstmann to larn to a greater extent than virtually unlike engagement in addition to fourth dimension classes of SQL packages.
To empathize the departure betwixt Timestamp in addition to Date better, allow me demo an representative of comparison Date in addition to Timestamp object inward Java:
So, you lot tin sack encounter they interruption the symmetry belongings of equals() method which says if a.equals(b) is truthful in addition to then b.equals(a) should likewise hold upwards true.
For those who needs a petty chip of revision virtually which Java type mapped to which SQL type inward database, hither is a dainty slide virtually Java to SQL type mapping:
So, fifty-fifty though both Date in addition to Timestamp contains engagement in addition to fourth dimension value in addition to their respective getMonth(), getHours() method doesn't throw IllegalArgumentException, you lot should non hold upwards using Timestamp inward house of java.util.Date due to the differences mentioned above. The inheritance human relationship betwixt Timestamp in addition to java.util.Date actually denotes implementation inheritance, in addition to non type inheritance, which is non a expert blueprint just sometimes finds its agency through fifty-fifty a good designed API similar JDK or JDBC.
Other JDBC Date in addition to Time articles you lot may like
Further Learning
JSP, Servlets in addition to JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 in addition to 2
Java Platform: Working alongside Databases Using JDBC
You cannot fifty-fifty compare Date to Timestamp class inward Java. The Timestamp.equals(Object) method returns simulated when you lot move past times an event of java.util.Date because the nanos constituent of a engagement is unknown. As a result, the Timestamp.equals(Object) method is non symmetric alongside abide by to the java.util.Date.equals(Object) method. Also, the hashCode method uses the underlying java.util.Date implementation in addition to thence does non include nanos inward its computation. See Core Java Volume two - Advanced Features past times Cay S. Horstmann to larn to a greater extent than virtually unlike engagement in addition to fourth dimension classes of SQL packages.
To empathize the departure betwixt Timestamp in addition to Date better, allow me demo an representative of comparison Date in addition to Timestamp object inward Java:
import java.sql.Timestamp; import java.util.Date; /* * Java Program to compare Date in addition to Timestamp inward JDBC */ public class TimeStampVsDate { public static void main(String[] args) { Date engagement = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); if (date.equals(timestamp)) { System.out .println("Both Date in addition to Timestamp are equal, when you lot compare using Date.equals() method"); } if (!timestamp.equals(date)) { System.out .println("But, they are non equal when you lot compare them using Timestamp.equals() method"); } } } Output Both Date and Timestamp are equal, when you lot compare using Date.equals() method But, they are not equal when you lot compare them using Timestamp.equals() method
So, you lot tin sack encounter they interruption the symmetry belongings of equals() method which says if a.equals(b) is truthful in addition to then b.equals(a) should likewise hold upwards true.
For those who needs a petty chip of revision virtually which Java type mapped to which SQL type inward database, hither is a dainty slide virtually Java to SQL type mapping:
So, fifty-fifty though both Date in addition to Timestamp contains engagement in addition to fourth dimension value in addition to their respective getMonth(), getHours() method doesn't throw IllegalArgumentException, you lot should non hold upwards using Timestamp inward house of java.util.Date due to the differences mentioned above. The inheritance human relationship betwixt Timestamp in addition to java.util.Date actually denotes implementation inheritance, in addition to non type inheritance, which is non a expert blueprint just sometimes finds its agency through fifty-fifty a good designed API similar JDK or JDBC.
Other JDBC Date in addition to Time articles you lot may like
- Difference betwixt java.util.Date in addition to java.sql.Date inward JDBC? (answer)
- Difference betwixt java.sql.Time, java.sql.Timestamp, in addition to java.sql.Date inward JDBC? (answer)
- How to convert java.util.Date to java.sql.Date inward JDBC? (example)
- How to convert java.sql.Date to java.util.Date inward JDBC? (example)
- How to convert java.util.Date to java.sql.Timestamp inward JDBC? (example)
- Why is String class made in conclusion inward Java? (answer)
- Why Hibernate Entity class should non hold upwards in conclusion inward Java? (answer)
Further Learning
JSP, Servlets in addition to JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 in addition to 2
Java Platform: Working alongside Databases Using JDBC
0 Response to "Why Timestamp Cannot Endure Used Inward House Of Appointment Inward Java?"
Post a Comment