Java Convert Timestamp to Date

In Java, you can convert a timestamp to a date using the java.sql.Timestamp class and the java.util.Date class.

Here’s an example code snippet that demonstrates how to convert a java.sql.Timestamp to a java.util.Date:

// Create a new timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());

// Convert the timestamp to a date
Date date = new Date(timestamp.getTime());

// Print the date
System.out.println(date);

In this example, we create a new java.sql.Timestamp object representing the current time using System.currentTimeMillis(). We then convert the timestamp to a java.util.Date object using the getTime() method, which returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the timestamp.

Finally, we print out the date using the System.out.println() method. The output will be in the format Fri May 07 13:21:23 GMT 2023.

Java Timestamp to Date Example:

In Java, you can convert a java.sql.Timestamp to a java.util.Date object using the getTime() method. Here’s an example code snippet that demonstrates how to do this:

// Create a new Timestamp object representing the current time
Timestamp timestamp = new Timestamp(System.currentTimeMillis());

// Convert the Timestamp to a Date
Date date = new Date(timestamp.getTime());

// Print the Timestamp and Date objects
System.out.println("Timestamp: " + timestamp);
System.out.println("Date: " + date);

In this example, we create a new Timestamp object representing the current time using System.currentTimeMillis(). We then convert the Timestamp to a Date object using the getTime() method, which returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the Timestamp.

Finally, we print out the Timestamp and Date objects using the System.out.println() method. The output will look something like this:

Timestamp: 2023-05-07 14:05:23.934
Date: Sat May 07 14:05:23 GMT 2023

Note that the exact format of the output may vary depending on your system’s locale settings.