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:
public long getTime()
Java Date to Timestamp Example
Let's see the simple example to convert Date to Timestamp in java.
Output:
You can format the Timestamp value using SimpleDateFormat class. Let's see the example of display Timestamp value without milliseconds.
Output: