如何在 JDBC 程序中将 Date 对象转换成时间戳?


java.sql.Date 类的 getTime() 方法从纪元时间 1970 年 00:00:00.000 格林威治标准时间以来的毫秒值为单位检索并返回当前时间戳中的时间(长整型)。

//Retrieving the date
Date date = rs.getDate("Dispatch_Date");

java.sql.Timestamp 类的构造函数接受代表从纪元时间以来的毫秒时间的长整型变量并构造时间戳对象。

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

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

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

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

//Creating a Statement object
Statement stmt = con.createStatement();

//Query to create a table
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 中检索日期值,将其转换成时间戳对象并打印详细信息。

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 {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Creating Statement object
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatch_data");
      //Retrieving values
      while(rs.next()) {
         System.out.println("Product Name: "+rs.getString("Product_Name"));
         System.out.println("Name Of The Customer: "+rs.getString("Name_Of_Customer"));
         //Retrieving the date
         Date date = rs.getDate("Dispatch_Date");
         //Printing the time of dispatch
         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

更新于: 2019 年 7 月 30 日

600 次浏览量

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.