Home > Java > javaTutorial > TimeStamp to Date in Java

TimeStamp to Date in Java

WBOY
Release: 2024-08-30 16:28:48
Original
611 people have browsed it

In Java timestamp is defined as a sequence for providing formatted or encoded data whenever any event occurs such as displaying date and time of day where this timestamp class can be converted to date class in java using Java.util library which needs to be imported when dealing with a timestamp in Java. In general, the timestamp is converted to date using the date class of Java.util class for displaying the SQL timestamp that contains both date and time value and there are several ways to convert timestamp to date where this timestamp can be obtained by using java.util.Date which belongs to java.util library.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Syntax of TimeStamp to Date in Java

There are 3 different ways to convert timestamp to date in java. Let us see the syntax of these different methods separately.

  • Using Constructor: One way for the conversion of the timestamp to date is done using the constructor of java.util.Date class. Let us see the syntax for this constructor:
Date ( long l)
Copy after login
  • Using Date Reference: Where there is timestamp class which extends the Date class where the instance of timestamp class is assigned to Date. Therefore the syntax for this description is given as follows:
Timestamp t = new Timestamp();
Date d = t;
Copy after login
  • Using Calendar Class: To obtain date from timestamp which is also present in calendar class will also provide date. Therefore this can be demonstrated in the syntax as:
Timestamp t = new Timestamp();
Calendar c = Calendar.getInstance();
Calendar.getTime();
Copy after login

How to Convert Timestamp to Date in Java?

In Java, the timestamp is defined as the time and the date format that is displayed using timestamp class which is done using java.util class for using date and calendar class whereas there is java.sql package for using timestamp class in java to implement timestamp in the java program. The conversion of the timestamp to date would result in displaying both date and time in milliseconds which is usually a human-readable format. Therefore exact working of the timestamp to date conversion is done using 3 different ways and is done using timestamp class using java.util or java.sql package.

1. Using Constructor

In Java, there is 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. The working of this method of conversion is firstly it will obtain the value which is long value as a parameter from the Date class constructor where it results in timestamp object which will convert long value using getTime() method

Example
import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = new Date(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using date constructor : ");
System.out.println(" \n ");
System.out.println( d );
}
}
Copy after login

Output:

TimeStamp to Date in Java

In the above example, we can see that we have first imported java.sql, Timestamp package, and java.util.Date package for using date constructor we need Date class and to use timestamp we use Timestamp class. Therefore, firstly we define and declare a variable “t” which belongs to the Timestamp class and we define it using “new” and we pass the currentTimeMills() function to display the timestamp which is later converted to date. Then we declare the Date variable of the Date class “d” which is defined using “new” where we are passing the getTime() function to convert the above timestamp into date format. Then using println we will display the converted date from the obtained timestamp. The output of the above program is as shown in the above screenshot.

2. Using Date References

This is another method of converting timestamp to date in Java using again the date class of java.util.date package. In this method, it just extends the date class where the timestamp instance is assigned to date directly and therefore the date object which results as output in the date format which was earlier in the timestamp. Let us see an example below for demonstrating such method in java:

Example
import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = t;
System.out.println(" Demonstration  of timestamp to date conversion using date reference : ");
System.out.println(" \n ");
System.out.println( d );
}
}
Copy after login

Output:

TimeStamp to Date in Java

In the above example, we can see it similar to the previous example but the only difference in the above program and this program is that instead of defining the date class here it will directly assign the timestamp instance “t” to date object “d”, here we do not use getTime() function as we are directly assigning the date object to timestamp instance and therefore here the output will look similar to timestamp but it is date format with time also including milliseconds. This can be seen in the above screenshot.

3. Using Calendar Class

Another method of converting timestamp to date in java is using calendar class which is rarely used by any developers. We can obtain calendar class by the java.util.Calendar package where we use this calendar class along with which we have to use getInstance() function to get timestamp instance and getTime() to get the date along with time which will be converted from the timestamp. Let us see an example below:

Example
import java.sql.Timestamp;
import java.util.Date;
import java.util.Calendar;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using Calendar class : ");
System.out.println(" \n ");
System.out.println(calendar.getTime());
}
}
Copy after login

Output:

TimeStamp to Date in Java

In the above example, we can see we have imported Calendar class also from java.util.Calendar package and this example are similar to the first example where we used date class. In this example, we define the calendar instance by using the getInstance() function which will be in the timestamp format later by using getTime() the timestamp instance is converted to a date object which results as shown in the output in the above screenshot.

Conclusion

In this article, we conclude that the timestamp to date conversion in java is very simple. In Java, there is java.util package which provides different classes for supporting this conversion in various ways with different classes. It depends on the user on which method to use according to the user’s requirement and time accuracy which displays the result accurately as required.

The above is the detailed content of TimeStamp to Date in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template