Related Articles

Related Articles

Java Program to Convert TimeStamp to Date
  • Last Updated : 02 Dec, 2020

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:

  1. Using constructors
  2. Using date reference
  3. Using Calendar class

Implementation: Describing approaches with the help of java program individually:

Method 1: Timestamp to Date Using Date constructor

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert timestamp to Date
  
// Importig java Date libraries
import java.sql.Timestamp;
import java.util.Date;
  
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the current system time and pasing 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);
    }
}

chevron_right


Output

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert timestamp to Date
// Importing java Date libraries
import java.sql.Timestamp;
import java.util.Date;
  
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the current system time and pasing 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);
    }
}

chevron_right


Output

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert timestamp to Date
  
// Importing java Date libraries
import 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 pasing 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());
    }
}

chevron_right


Output

Tue Nov 24 00:28:31 UTC 2020

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :