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