×

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970) which can pass to java.sql.Timestamp. It creates a new instance of the Timestamp object in JDBC. The only way to create a Timestamp instance is to create the constructor of Timestamp class. It accepts a long value as an argument. So you need to convert Timestamp into long value using getTime() method of Timestamp class. You can also format the output by using java.text.SimpleDateFormat class. The Timestamp class is a subclass of Date class and has additional nanoseconds values, required to hold the SQL Timestamp fractional second value. In the implementation of the Timestamp class, you will find that only integral seconds stored in the java.util.Date component. The nanoseconds are separated. The signature of getTime() method of date class is: public long getTime() Example In the following example, we have created a constructor of Timestamp class and parse a method currentTimeMillis() of System class  as an argument. It returns the current time in milliseconds. The object of Timestamp class invokes the getTime() method. The  getTime() method returns the number of millisecond from the Epoch and stores these milliseconds into the object of Date class. The println statement prints the date.
import java.util.Date;    
import java.sql.Timestamp;    
public class TimestampToDateExample
 {    
public static void main(String args[])
{    
Timestamp tstamp=new Timestamp(System.currentTimeMillis());  
Date d=new Date(tstamp.getTime());  
System.out.println(d);                     
}
}
Output
Wed May 29 13:59:06 IST 2019
You can assign  instance of Timestamp class into Date because Timestamp class extends Date class. When you assign instance of Timestamp into Date, the Date object will be like Timestamp. It removes the milliseconds. Example In the following example, we have done the same as in previous example except one thing is that we have assigned instance of Timestamp class into Date.
import java.util.Date;    
import java.sql.Timestamp;    
public class TimestampToDateExample1
{    
public static void main(String args[])
{    
Timestamp tstamp=new Timestamp(System.currentTimeMillis());  
Date d=tstamp;  
System.out.println(d);                     
}
}
Output
2019-05-29 14:05:52.058

Related Topics

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.