如何在 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.