java.sql.Timestamp in java example. Convert java.util.Date to java.sql.Timestamp.

This tutorial covers about why we need java.sql.Timestamp type and also covers how to convert the java.sql.Date into java.sql.Timestamp. As we know, JDBC DATE , JDBC TIME and JDBC TIMESTAMP are the three JDBC types related to date/time. JDBC TIME represents time only i.e it has  hours, minutes, and seconds but does not have nanoseconds. To store time with more accuracy, it is important to have the nanoseconds field in date/time. JDBC TIMESTAMP type represents DATE plus TIME plus a nanosecond field. The corresponding SQL TIMESTAMP type is defined in SQL-92. The standard Java class java.util.Date does not exactly match with JDBC TIMESTAMP type because java.util.Date includes both DATE and TIME information but does not have nanoseconds. So we need a date/time type to map with JDBC TIMESTAMP .

JDBC defines a class called java.sql.Timestamp that extends java.util.Date. This type is a composite of a java.util.Date (date and time component) and a separate nanoseconds value. This separate nanos allows the JDBC API to identify this as an SQL TIMESTAMP value. Only integral seconds are stored in the java.util.Date component. The fractional seconds ( the nanos) are separate . Now let us see some of the methods of java.sql.Timestamp type.

Constructor :

Timestamp(long time) :Constructs a Timestamp object using a milliseconds time value.

eg. Timestamp ts = new Timestamp(95432573287540L);

Timestamp ts1 = new Timestamp(69754573285765L);

Methods :

boolean after(Timestamp ts) – to check whether this Timestamp object is later than the given Timestamp object. eg. ts.before(ts1) returns false

boolean before(Timestamp ts) : to check whether this Timestamp object is earlier than the given Timestamp object. eg. ts.after(ts1) returns true.

int compareTo(Date o / Timestamp ts) : to compare this Timestamp object to the given Date (must be a TimeStamp object) / Timestamp object. Returns 1 if this timestamp value is greater than the given timestamp, -1 , if this timestamp value is less than the given timestamp, 0 if both are equal. eg. ts.compareTo(ts1) returns 1

boolean equals(Object ts / Timestamp ts ) : to check this Timestamp object is equal to the given object / Timestamp

int getNanos() : returns the Timestamp object’s nanoseconds value. eg. ts.getNanos() returns 540000000

long getTime() : returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object. eg. ts.getTime() returns 95432573287540

void setNanos(int n) : Sets this Timestamp object’s nanoseconds field to the given value. eg. ts.setNanos(550000543);

void setTime(long time) : Sets this Timestamp object to represent milliseconds

String toString() : formats timestamp in JDBC timestamp escape format.  ts.toString() returns 4994-02-19 21:38:07.550000543

static Timestamp valueOf(String s) : Converts a String object in JDBC timestamp escape format to a Timestamp value.

The following code shows how to use the the methods of java.sql.Timestamp.

Conversion of java.util.Date to java.sql.TimeStamp

The following code accepts the java.util.Date and converts to SQL Timestamp

public static Timestamp utilDateToSQLTimestamp(java.util.Date date) {
  try {
      return new Timestamp(date.getTime());
    } catch (Exception e) { return null;}
 }

Conversion of java.sql.TimeStamp to java.util.Date

The following code accepts the java.sql.Timestamp and converts to java.utl.Date

public static java.util.Date timestampToUtilDate(Timestamp ts) {
  try {
        return new java.util.Date(ts.getTime());
     } catch (Exception e) { return null;  }
 }

 

Conversion of java.sql.TimeStamp to java.sql.Date

The following code accepts the java.sql.Timestamp and converts to java.sql.Date

public static Date timestampToSQLDate(Timestamp ts) {
  try {
        return new Date(ts.getTime());
  } catch (Exception e) { return null; }
 }

Conversion of java.sql.TimeStamp to java.util.Date that is accurate to within one millisecond

When you use java.sql.Timestamp where a java.util.Date object is expected, the nanoseconds component is lost. There is a way to convert java.sql.Timestamp object to a java.util.Date object that is accurate to within one millisecond. java.util.Date object is stored with a precision of one millisecond. This one millisecond can be utilized to store the result of converting the nanoseconds in the nanoseconds component to whole milliseconds (by dividing the number of nanoseconds by 1,000,000). Nanoseconds value may be lost, but the resulting java.util.Date object will be accurate to within one millisecond. The following code converts a java.sql.Timestamp object to a java.util.Date object that is accurate to within one millisecond.

public static java.util.Date timestampToUtilDateOccurate(Timestamp ts) {
  try {
        return new java.util.Date(ts.getTime() + (t.getNanos() / 1000000));
  } catch (Exception e) { return null;  }
 }

Note:  The Timestamp.equals(Object) method returns always false when we pass java.util.Date value because the nanoseconds component of a date is unknown.

Output :

Also view my earlier post to convert Java.util.Date to java.sql.Date and vice versa

Leave a Reply