Java Convert Date to Timestamp

Java Convert Date to Timestamp

We can convert Date to Timestamp in java using constructor of java.sql.Timestamp class.

The constructor of Timestamp class receives long value as an argument. So you need to convert date into long value using getTime() method of java.util.Date class.

You can also format the output of Timestamp using java.text.SimpleDateFormat class.

Constructor of Timestamp class:

getTime() method of Date class:

Java Date to Timestamp Example

Let's see the simple example to convert Date to Timestamp in java.

Test it Now

Output:

2017-11-02 01:59:30.274

You can format the Timestamp value using SimpleDateFormat class. Let's see the example of display Timestamp value without milliseconds.

Test it Now

Output:

2017-11-02 02:04:03




Latest Courses