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 🙂