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 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
import java.sql.Timestamp;
import java.util.Date;
class GFG {
public static void main(String[] args)
{
Timestamp ts
= new Timestamp(System.currentTimeMillis());
Date date = new Date(ts.getTime());
System.out.println(date);
}
}
|
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
import java.sql.Timestamp;
import java.util.Date;
class GFG {
public static void main(String[] args)
{
Timestamp ts
= new Timestamp(System.currentTimeMillis());
Date date = ts;
System.out.println(date);
}
}
|
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
import java.sql.Timestamp;
import java.util.Date;
import java.util.Calendar;
class GFG {
public static void main(String[] args)
{
Timestamp timestamp
= new Timestamp(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());
System.out.println(calendar.getTime());
}
}
|
Output
Tue Nov 24 00:28:31 UTC 2020
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!