Java Data Type How to - Convert Date to timestamp








Question

We would like to know how to convert Date to timestamp.

Answer

import java.sql.Timestamp;
import java.util.Date;
// w  w  w  .jav  a2  s.co m
public class Main {
  public static void main(String[] args) throws Exception {
    Date date = new Date();
    System.out.println("Format To times:");
    System.out.println(date.getTime());
    Timestamp ts = new Timestamp(date.getTime());
    System.out.println(ts);
  }
}

The code above generates the following result.