Java Data Type How to - Convert java.util.Date to java.sql.Timestamp








Question

We would like to know how to convert java.util.Date to java.sql.Timestamp.

Answer

import java.sql.Timestamp;
import java.util.Date;
//  w  ww .ja v a  2 s .c  o  m
public class Main {

  public static void main(String[] args) {
    System.out.println(convertToSqlTimestamp(new Date()));
  }

  public static java.sql.Timestamp convertToSqlTimestamp(Date date) {
    return new Timestamp(date.getTime());
  }
}

The code above generates the following result.