如何在 JDBC 程式中把時間戳物件轉換為 Date?
Timestamp 類的時間(getTime())方法檢索並返回當前時間戳自格林尼治標準時間 1970 年 1 月 1 日 00:00:00.000 以來的毫秒數(長期)時間。
Timestamp timestamp = rs.getTimestamp("DispatTimestamp");
long time = timestamp.getTime();java.sql.Date 類建構函式接受表示時間自紀元時間以來的毫秒數的長時間變數,並構造日期物件。
//Printing the date of dispatch
System.out.println("Date of dispatch: "+new Date(time));利用這些,您可以在 JDBC 中轉換時間戳物件到日期物件。
假設我們已經使用如下的語句物件建立與 MySQL 資料庫的連線,並建立了一個名為 dispatch_data 的表
//Creating a Statement object
Statement stmt = con.createStatement();
//Query to create a table
String create_query = "Create table disptach_data ("
+ "Product_Name VARCHAR(255), "
+ "Name_Of_Customer VARCHAR(255) , "
+ "Dispatch_Timestamp timestamp, "
+ "Location VARCHAR(255) )";
stmt.execute(create_query);我們已使用 PreparedStatement 填充該表,如下所示
String query = "INSERT INTO dispatch_data VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "KeyBoard");
pstmt.setString(2, "Amith");
pstmt.setTimestamp(3, new Timestamp(1567296000000L));
pstmt.setString(4, "Hyderabad");
pstmt.execute();
pstmt.setString(1, "Earphones");
pstmt.setString(2, "Sumith");
pstmt.setTimestamp(3, new Timestamp(1556668800000L));
pstmt.setString(4, "Vishakhapatnam");
pstmt.execute();
pstmt.setString(1, "Mouse");
pstmt.setString(2, "Sudha");
pstmt.setTimestamp(3, new Timestamp(1551398399000L));
pstmt.setString(4, "Vijayawada");
pstmt.execute();
System.out.println("Records inserted......");以下 JDBC 程式檢索 ResultSet 中的時間戳值,將其轉換為 Date 和 Time 物件,並列印詳細資訊。
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
public class TimeStampToDate {
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 timestamp
Timestamp timestamp = rs.getTimestamp("Dispatch_Timestamp");
//Printing the date of dispatch
System.out.println("Date of dispatch: "+new Date(timestamp.getTime()));
//Printing the time of dispatch
System.out.println("Time Of Dispatch: "+new Time(timestamp.getTime()));
System.out.println();
}
}
}輸出
Connection established...... Product Name: KeyBoard Name Of The Customer: Amith Date of dispatch: 2019-09-01 Time Of Dispatch: 05:30:00 Product Name: Ear phones Name Of The Customer: Sumith Date of dispatch: 2019-05-01 Time Of Dispatch: 05:30:00 Product Name: Mouse Name Of The Customer: Sudha Date of dispatch: 2019-03-01 Time Of Dispatch: 05:29:59
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP