How to convert Timestamp to Date in Java

By | 2019-04-10T19:45:41+05:30 April 10th, 2019|java|

Here we will see how to convert Timestamp to Date in java in different ways.

Convert Timestamp to Date:

We can convert the java.sql.Timestamp to java.util.Date in different ways in Java, let’s see the possibilities.

1. Timestamp to Date Using Date constructor:

We can convert the Timestamp to Date by passing a Timestamp object to Date constructor like the following.

// Using date constructor
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(timestamp.getTime());
System.out.println(date);

Output:

Tue Apr 09 17:52:48 IST 2019

2. Timestamp to DateUsing Date Reference:

We can even get the Date type by simply assigning the Timestamp object to Date reference.

Note: java.sql.Timestamp class extends from the java.util.Date class.
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = timestamp;
System.out.println(date);

Output:

2019-04-09 17:52:48.292

3. Timestamp to Date Using Calendar class:

// Using Calendar
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime() );
System.out.println(calendar.getTime());

Output:

Tue Apr 09 17:52:48 IST 2019

References:

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment