Java Program to Convert TimeStamp to Date - GeeksforGeeks

17 Jan, 2022
Date Time class is used to display date and time and manipulate date and time in java and in addition to this it is also used for formatting date and time class in java across time zone associated data. So in order to import this class from a package called java.utils
Timestamp class can be converted to Date class in java using the Date class which 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(present in SQL package).
In order to import date class syntax is as follows: import java.util.Date ;
After importing this class one can create an object of the Date class in order to print the current date and time. Now in order to print the default date and time simply call the print command using toString() method to get the current date and time 
Note: In order to print no of milliseconds till now simply use getTime() instead of to String() to get no of milliseconds till the program is being executed. Also, remember 01-01-1970 is the base reference also referred to as Epoch time.
Methods: There are 3 ways to do so as listed below:
Using constructorsUsing date referenceUsing Calendar class
Using constructors
Using date reference
Using Calendar class
Implementation: Describing approaches with the help of java program individually:
Method 1: Timestamp to Date Using Date constructor
Java
// Java program to convert timestamp to Date // Importing java Date librariesimport java.sql.Timestamp;import java.util.Date; class GFG {     // Main driver method    public static void main(String[] args)    {        // Getting the current system time and passing it        // to constructor of time-stamp class        // Using currentTimeMillis        Timestamp ts            = new Timestamp(System.currentTimeMillis());         // Passing the long value in the Date class        // constructor        Date date = new Date(ts.getTime());         // Printing the date        System.out.println(date);    }}
  
Tue Nov 24 00:28:31 UTC 2020
Method 2: Timestamp to Date Using Date Reference
 
The Timestamp class extends the Date class. So, you can directly assign an instance of the Timestamp class to Date. In such a case, the output of the Date object will be like Timestamp ie it would be in the format like date followed by time (to the scale of milliseconds).
 
Java
// Java program to convert timestamp to Date// Importing java Date librariesimport java.sql.Timestamp;import java.util.Date; class GFG {     // Main driver method    public static void main(String[] args)    {        // Getting the current system time and passing it        // to constructor of time-stamp class        Timestamp ts            = new Timestamp(System.currentTimeMillis());         /* Date class is super class of TimeStamp class*/         //  Directly assigning the object of        // timestamp class to date class        Date date = ts;         // Printing the date        System.out.println(date);    }}
  
2020-11-24 00:28:30.646
Method 3: Timestamp to Date Using Calendar class
 
We can also use calendar class to get the Date using Timestamp, below is the implementation of the approach to convert timestamp to date.
 
Java
// Java program to convert timestamp to Date // Importing java Date librariesimport java.sql.Timestamp;import java.util.Date;import java.util.Calendar; class GFG {     // Main driver method    public static void main(String[] args)    {        // Getting the current system time and passing it        // to constructor of time-stamp class        Timestamp timestamp            = new Timestamp(System.currentTimeMillis());         // Getting the calendar class instance        Calendar calendar = Calendar.getInstance();         // Passing the long value to calendar class function        calendar.setTimeInMillis(timestamp.getTime());         // Getting the time using getTime() function        System.out.println(calendar.getTime());    }}
  
Tue Nov 24 00:28:31 UTC 2020
 
sweetyty
Java-Date-Time
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?