如何在JDBC程序中将Date对象转换为Timestamp?

java.sql.Date类的getTime()方法从当前时间戳记(从格林尼治标准时间1970年1月00:00:00.000)以毫秒(长)为单位检索并返回当前时间。

//检索日期
Date date = rs.getDate("Dispatch_Date");

java.sql.Timestamp类的构造函数接受一个长变量,该变量代表从纪元时间起以毫秒为单位的时间,并构造Timestamp对象。

//Creating a Timestamp object.
Timestamp ts = new Timestamp(date.getTime()));

使用这些,您可以在JDBC中将Date对象转换为TimeStamp对象。

假设我们已经建立了与MySQL数据库的连接,并使用如下语句对象创建了一个名为dispatch_data的表:

假设我们已经建立了与MySQL数据库的连接,并使用如下语句对象创建了一个名为dispatch_data的表:

//创建一个Statement对象
Statement stmt = con.createStatement();

//查询创建表
String create_query = "Create table dispatch_data ("
   + "Product_Name VARCHAR(255), "
   + "Name_Of_Customer VARCHAR(255) , "
   + "Dispatch_Date date, "
   + "Location VARCHAR(255) )";
stmt.execute(create_query);
System.out.println("table created......");

我们使用PreparedStatement将表填充为:

//Inserting values to a table
String query = "INSERT INTO dispatch_data VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);

pstmt.setString(1, "KeyBoard");
pstmt.setString(2, "Amith");
pstmt.setDate(3, new Date(376401869000L));
pstmt.setString(4, "Hyderabad");
pstmt.execute();

pstmt.setString(1, "Ear phones");
pstmt.setString(2, "Sumith");
pstmt.setDate(3, new Date(356788333000L));
pstmt.setString(4, "Vishakhapatnam");
pstmt.execute();

pstmt.setString(1, "Mouse");
pstmt.setString(2, "Sudha");
pstmt.setDate(3, new Date(594733933000L));
pstmt.setString(4, "Vijayawada");
pstmt.execute();

System.out.println("Records inserted......");

以下JDBC程序从ResultSet中检索日期值,然后将其转换为Timestamp对象并打印详细信息。

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class DateToTimeStamp {
   public static void main(String args[])throws Exception {
      //注册驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //获得连接
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //创建一个Statement对象
      Statement stmt = con.createStatement();
      //创建Statement对象
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatch_data");
      //检索值
      while(rs.next()) {
         System.out.println("Product Name: "+rs.getString("Product_Name"));
         System.out.println("Name Of The Customer: "+rs.getString("Name_Of_Customer"));
         //检索日期
         Date date = rs.getDate("Dispatch_Date");
         //打印发货时间
         System.out.println("Dispatch_Timestamp: "+new Timestamp(date.getTime()));
         System.out.println();
      }
   }
}

输出结果

Connection established......
Product Name: KeyBoard
Name Of The Customer: Amith
Dispatch_Timestamp: 1981-12-05 00:00:00.0

Product Name: Ear phones
Name Of The Customer: Sumith
Dispatch_Timestamp: 1981-04-22 00:00:00.0

Product Name: Mouse
Name Of The Customer: Sudha
Dispatch_Timestamp: 1988-11-05 00:00:00.0