In this guide, we will learn how to convert Timestamp to Date. Timestamp has higher precision than date and is used when we want to include the fractions seconds in Date & Time.
In order to convert the Timestamp to Date, we can pass the milliseconds value while creating an object of Date class.
Date date = new Date(long time)
This long type time
argument represents the milliseconds value since January 1, 1970, 00:00:00 GMT. To get this milliseconds value from the given Timestamp, we can use the getTime()
method of Timestamp class.
public long getTime()
Program to Convert Timestamp to Date
Let’s write the java program based on the above information:
import java.sql.Timestamp; import java.util.Date; public class JavaExample { public static void main(String args[]) { //getting the current Timestamp from system Timestamp ts = new Timestamp(System.currentTimeMillis()); //Timestamp to Date Date date = new Date(ts.getTime()); System.out.println("Timestamp is: "+ts); System.out.println("Date is: "+date); } }
Output:

You can display the converted date in various different formats as shown below:
import java.sql.Timestamp; import java.util.Date; import java.text.SimpleDateFormat; public class JavaExample { public static void main(String args[]){ Timestamp ts = new Timestamp(System.currentTimeMillis()); Date date = new Date(ts.getTime()); //Converted Date in various formats SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); System.out.println(sdf.format(date)); //Day Name SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM YY hh:mm:ss"); System.out.println(sdf2.format(date)); //include AM and PM SimpleDateFormat sdf3 = new SimpleDateFormat("EEE HH:mm:ss aa"); System.out.println(sdf3.format(date)); } }
Output:

References: Date and Timestamp documentation