Thursday, April 18, 2024
HomeJavaWhy Timestamp can't be used rather than Date in Java? Instance

Why Timestamp can’t be used rather than Date in Java? Instance


One of many difficult questions from the Java Interview is, “Can we cross a Timestamp occasion to a way anticipating java.util.Date?, it is a difficult query as a result of the reply is each Sure and No. You may, in fact, cross a Timestamp object to a way with the argument as Date as a result of first, Timestamp is a subclass of java.util.Date and second it incorporates each date and time values that are lacking in both java.sql.Date and java.sql.Time. So there’s extra motive to cross a Timestamp worth to Date however you shouldn’t be doing that. Why? as a result of Timestamp is just not precisely Date.

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:

Why Timestamp cannot be used as Date in Java JDBC

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.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments