How to Convert Timestamp to Date in Java

You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970) which can pass to java.sql.Timestamp. It creates a new instance of the Timestamp object in JDBC.

The only way to create a Timestamp instance is to create the constructor of Timestamp class. It accepts a long value as an argument. So you need to convert Timestamp into long value using getTime() method of Timestamp class. You can also format the output by using java.text.SimpleDateFormat class.

The Timestamp class is a subclass of Date class and has additional nanoseconds values, required to hold the SQL Timestamp fractional second value. In the implementation of the Timestamp class, you will find that only integral seconds stored in the java.util.Date component. The nanoseconds are separated.

The signature of getTime() method of date class is:

public long getTime()


In the following example, we have created a constructor of Timestamp class and parse a method currentTimeMillis() of System class  as an argument. It returns the current time in milliseconds. The object of Timestamp class invokes the getTime() method. The  getTime() method returns the number of millisecond from the Epoch and stores these milliseconds into the object of Date class. The println statement prints the date.


You can assign  instance of Timestamp class into Date because Timestamp class extends Date class. When you assign instance of Timestamp into Date, the Date object will be like Timestamp. It removes the milliseconds.


In the following example, we have done the same as in previous example except one thing is that we have assigned instance of Timestamp class into Date.


Pin It on Pinterest

Share This