Date and Time

How to Convert a Date to Timestamp in java

Table of Contents

Program

import java.sql.Timestamp;    
import java.util.Date;    
 public class DateTimestamp {    
       public static void main(String args[]){    
                Date date = new Date();  
                Timestamp ts=new Timestamp(date.getTime());  
                System.out.println(ts);                     
        }    
} 

Output

2021-04-11 23:21:05.356

Description

public long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.

Overrides:

getTime in class Date

Returns:

the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.