
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Timestamp to Date in Java
In Java, Timestamp can be converted Date by using Date class. Date class is present in java.util package. The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class.
Let's deep dive into this article, to know how it can be done by using Java programming language.
To show you with instance
Suppose the timestamp is 06/01/2023.
Then corresponding Date is "Fri Jan 06 19:51:09 GMT 2023".
Algorithm
Algorithm - 1 (By Using Constructors)
Step-1: Take a default Timestamp from the system.
Step-2: Pass it to the constructor of the time-stamp class Using currentTimeMillis.
Step-3: Now pass the long value in the Date class constructor.
Step-4: Print the result i.e. Date value.
Algorithm - 2 (By Using Date Reference)
Step-1: Take a default Timestamp from the system.
Step-2: Directly assign the object of timestamp class to date class as Date class is a super class of TimeStamp class.
Step-3: Print the result i.e. Date value.
Algorithm - 3 (By Using Calendar Class)
Step-1: Take a default Timestamp from the system.
Step-2: Get the calendar class instance.
Step-3: Now Pass the long value to the calendar class function.
Step-4: Get the time using getTime() function
Step-5: Print the result i.e. Date value.
Syntax
setTimeMillis() Method: It is the method of calendar class and used to set current time in milliseconds.
getTime() Method: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by the Date object.
Multiple Approaches
We have provided the solution in different approaches.
By Using constructors
By Using Date Reference
By Using Calendar class
Let's see the program along with its output one by one.
Approach-1: By Using Constructors
In this approach, java.util.Date class which is provided by the java.util package and we will be using the constructor of this class to convert timestamp to date.
Example
import java.sql.Timestamp; import java.util.Date; public class Main { // Main method public static void main(String[] args) { //taking current time from system Timestamp tms = new Timestamp(System.currentTimeMillis()); // Passing the value in the Date class constructor Date date = new Date(tms.getTime()); // Printing the date value System.out.println(date); } }
Output
Thu Aug 17 07:01:40 GMT 2023
Approach-2: By Using Date Reference
In this approach, we take date input from the system. Then we use the date class of java.util.date package. And directly assign the object of timestamp class to date class.
Example
import java.sql.Timestamp; import java.util.Date; public class Main { // Main method public static void main(String[] args) { //taking current time from system Timestamp tms = new Timestamp(System.currentTimeMillis()); //Direct assigning of object of timestamp class to date class Date date = tms; //Printing the date value System.out.println(date); } }
Output
2023-08-17 07:04:03.359
Approach-3: By Using Calendar Class
In this approach, we take date input from the system. Then we use the Calendar class to obtain Date from timestamp.
Example
import java.sql.Timestamp; import java.util.Date; import java.util.Calendar; public class Main { // Main method public static void main(String[] args) { //taking current time from system Timestamp ts = new Timestamp(System.currentTimeMillis()); //Getting the calendar class instance Calendar calendar = Calendar.getInstance(); // Passing the long value to calendar class function calendar.setTimeInMillis(ts.getTime()); //printing the time using getTime() function System.out.println(calendar.getTime()); } }
Output
Thu Aug 17 07:05:15 GMT 2023
In this article, we explored different approaches to convert Timestamp to Date by using Java programming language.