In this guide, you 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. There are two ways you can do this:
- Using java.util.Date
- Using java.time
Timestamp to Date Conversion in Java
1. Using java.util.Date
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)
. Let’s write the complete program:
import java.util.Date;
public class TimestampToDate {
public static void main(String[] args) {
// Example timestamp
// This represents a specific date and time
long timestamp = 1629385200000L;
// Convert timestamp to Date
Date date = new Date(timestamp);
// Print the Date
System.out.println(date);
}
}
How to display the Date in different formats:
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:
2. Using java.time (Java 8 and later)
The java.time
package is introduced in Java 8. It offers various functionalities for date and time manipulation. Let’s see how can we use this package for Timestamp to Date Conversion.
new Date(timestamp)
: Converts the timestamp to aDate
object.Instant.ofEpochMilli(timestamp)
: Converts the timestamp to anInstant
.LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
: Converts theInstant
to aLocalDateTime
using the system’s default time zone.DateTimeFormatter
: Used to format theLocalDateTime
into a human-readable string.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class TimestampToDate {
public static void main(String[] args) {
// Example timestamp
long timestamp = 1629385200000L;
// Convert timestamp to Instant
Instant instant = Instant.ofEpochMilli(timestamp);
// Convert Instant to LocalDateTime (in system default timezone)
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
// Print the LocalDateTime
System.out.println(dateTime);
// Format and print the LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dateTime.format(formatter));
}
}
Both the approaches discussed in this article produce desired result. Choose the approach that fits your needs. The java.time
package is recommended as it offers modern approach, which may receive further updates in future.