While manipulating DateTime values, Java users often come across a situation where they need to convert a timestamp to a date. Timestamps are usually represented in numerical values that are difficult to read. Therefore, converting timestamps to dates allows us to display DateTime values in a human-friendly format. To do this in Java, we can use the built-in classes like Date and Calendar.
So, let’s learn how to convert a timestamp to a date in Java using different methods.
How to Convert a Timestamp to a Date in Java
We can use the “Date()” constructor, “Calendar” class, or “Date” Reference for converting a timestamp to a date in Java.
So let’s get started with the Date() constructor.
Method 1: Converting a Timestamp to a Date Using Date() Constructor
The Date class in Java offers multiple constructors and built-in methods that let us work with the DateTime values efficiently. We can pass a timestamp to the Date() constructor to convert it into a date. Here is an example:
import java.sql.Timestamp;
import java.util.Date;
public class Example {
public static void main(String[] args) {
Timestamp currTS = new Timestamp(System.currentTimeMillis());
System.out.println("The Current Timestamp == " + currTS);
System.out.println("The Type of Original DateTime == " + currTS.getClass().getName());
Date currDate = new Date(currTS.getTime());
System.out.println("The Current Date == " + currDate);
System.out.println("The Type of Converted DateTime == " + currDate.getClass().getName());
}
}
In this code,
- First, we import the Timestamp and Date classes from the sql and util packages, respectively.
- In the main() method, we wrap the “currentTimeMillis()” method within the Timestamp() constructor to get the Current DateTime.
- Next, we print the current date, time, and its respective data type on the console.
- After this, we pass the current timestamp to the Date() constructor to convert it into a date. It is important to note that the Date() constructor accepts a long-type value, so we use the getTime() method with the Timestamp instance to convert it into a long-type value.
- Finally, we print the converted date and its respective data type on the console:
Method 2: Converting a Timestamp to a Date Using Calendar Class
Calendar is a built-in abstract class in Java that provides different methods to convert dates between a particular instant in time and calendar fields like, Day, Month, Year, etc. We can use this class to convert a timestamp to a date, as demonstrated in the following code:
import java.sql.*;
import java.util.*;
public class Example {
public static void main(String[] args) {
Timestamp currTS = new Timestamp(System.currentTimeMillis());
System.out.println("The Current Timestamp == " + currTS);
System.out.println("The Type of Original DateTime == " + currTS.getClass().getName());
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currTS.getTime());
System.out.println("The Current DateTime == " + cal.getTime());
System.out.println("The Type of Converted DateTime == " + cal.getClass().getName());
}
}
In this code,
- We import the essential classes like Timestamp and Calendar from the respective packages.
- Next, we use the Timestamp() constructor to get a timestamp that we want to convert.
- We print the retrieved timestamp and its respective class on the console.
- After this, we create a Calendar class instance and set its time to the timestamp using the setTimeInMillis() method.
- Finally, we print the converted date and its corresponding class on the console:
Method 3: Converting a Timestamp to a Date Using a Date Reference
We can directly assign a timestamp object to a date instance to convert a timestamp to a date. Here is an example:
import java.sql.Timestamp;import java.util.Date;
public class Example {
public static void main(String[] args) {
Timestamp currTS = new Timestamp(System.currentTimeMillis());
System.out.println("The Current Timestamp == " + currTS);
Date currDate = currTS;
System.out.println("The Current Date == " + currDate);
}
}
In this code, first, we get the current DateTime using the Timestamp() constructor and print it on the console. After this, we assign the obtained current timestamp to the Date instance. Finally, we print the converted date on the console:
Important: We can easily convert a timestamp to a date using the Date reference, however, Java experts recommend not using this method due to the potential loss of milliseconds and nanoseconds.
How to Convert a Timestamp to a Specific Date Format
We can use the SimpleDateFormat() constructor to format the converted date into a specific date format, as shown in the following example:
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Example {
public static void main(String[] args) {
Timestamp currTS = new Timestamp(System.currentTimeMillis());
System.out.println("The Current Timestamp == " + currTS);
System.out.println("The Type of Original DateTime == " + currTS.getClass().getName());
Date currDate = new Date(currTS.getTime());
DateFormat formattedDate = new SimpleDateFormat("yyyy/MM/dd");
String date = formattedDate.format(currDate);
System.out.println("The Formatted Date == " + date);
System.out.println("The Type of Converted DateTime == " + currDate.getClass().getName());
}
}
In this code,
- First, we import the required classes like Timestamp, DateFormat, Date, and SimpleDateFormat from the corresponding packages.
- Next, we get the timestamp to be converted using the Timestamp() constructor and print it on the console. Also, we print the class name to which the current timestamp belongs on the console.
- After this, we pass the given timestamp to the Date() constructor to convert it into a date.
- Next, we use the SimpleDateFormat class to format the converted date into “yyyy/MM/dd” format.
- Finally, we print the formatted date along with its respective class on the console:
How to Convert a Date to a Timestamp in Java
We can also convert a date to a timestamp in Java using the Timestamp() constructor, as shown in the example below:
import java.sql.Timestamp;
import java.util.Date;
public class Example {
public static void main(String[] args) {
Date currDate = new Date(); System.out.println("The Current Date == " + currDate); System.out.println("The Type of Original DateTime == " + currDate.getClass().getName());
Timestamp currTS = new Timestamp(currDate.getTime());
System.out.println("The Converted Timestamp == " + currTS);
System.out.println("The Type of Converted DateTime == " + currTS.getClass().getName());
}
}
In this code,
- We get the current Date using the Date() constructor and print it on the console along with its respective class.
- Next, we invoke the “getTime()” method with the Date instance and pass it to the Timestamp() constructor to convert it into a timestamp.
- In the end, we print the converted timestamp and its data type on the console:
That’s all about converting a timestamp to a date(or vice versa) in Java.
Conclusion
We can use the Date() constructor, Calendar class, and date reference to convert a timestamp to a date in Java. To convert a timestamp to a date, simply invoke the getTime() method with the timestamp instance and pass it as an argument to the Date() constructor. In addition to this, we can convert a timestamp to a date and format it according to a specific format pattern using the SimpleDateFormat class. This post demonstrated different methods of converting a timestamp to a date in Java using suitable examples.