如何使用 JDBC 从 java.sql.Date 获取 LocalDateTime 对象?

jdbcobject oriented programmingprogramming

Java8java.time 包提供了一个名为 LocalDateTime 的类,用于获取本地日期和时间的当前值。除了日期和时间值之外,您还可以使用它来获取其他日期和时间字段,例如年中的某天、星期几和星期几。

将 java.sql.Date 转换为 LocalDateTime

java.sql.TimeStamp 类提供了一个名为 toLocalDateTime() 的方法,此方法将当前时间戳对象转换为 LocalDateTime 对象并返回它。

将日期转换为 LocalDateTime 对象。

  • 使用 getTime() 方法作为 − 从 Date 对象创建 Timestamp 对象
Date date = rs.getDate("DispatchDate");
//将日期转换为时间戳
Timestamp timestamp = new Timestamp(date.getTime());
  • 现在,使用 toLocalDateTime() 方法将 Timestamp 对象转换为 LocalDateTime 对象。
Time time = rs.getTime("DeliveryTime");
//将时间转换为时间戳
Timestamp timestamp = new Timestamp(time.getTime());
//将时间戳转换为 LocalDateTime
timestamp.toLocalDateTime();

示例

让我们使用 CREATE 语句在 MySQL 数据库中创建一个名为 dispatches 的表,如下所示 −

CREATE TABLE dispatches(
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(255)
);

现在,我们将使用 INSERT 语句 − 在 dispatches 表中插入 2 条记录

insert into dispatches values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into dispatches values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');

以下 JDBC 程序与数据库建立连接并检索 dispatches_data 表的内容,并将第一条记录的日期值(Dispatch_Date 列)转换为 LocalDateTime 对象并将其与其内容一起显示。

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
public class LocalDateTimeExample {
   public static void main(String args[]) throws SQLException {
      //注册驱动程序
      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 stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatches");
      rs.next();
      //从表中检索日期
      Date date = rs.getDate("DispatchDate");
      //将日期转换为时间戳
      Timestamp timestamp = new Timestamp(date.getTime());
      System.out.println("LocalDateTime value from date: "+timestamp.toLocalDateTime());
      System.out.println("");
      System.out.println("Contents of the first record: ");
      System.out.println("Product Name: "+rs.getString("ProductName"));
      System.out.println("Customer Name: "+rs.getString("CustomerName"));
      System.out.println("Dispatch Date: "+rs.getDate("DispatchDate"));
      System.out.println("Delivery Time: "+ rs.getTime("DeliveryTime"));
      System.out.println("Location: "+rs.getString("Location"));
      System.out.println();
   }
}

输出

Connection established......
LocalDateTime value from date: 2019-09-01T00:00
Contents of the first record:
Product Name: Key-Board
Customer Name: Raja
Dispatch Date: 2019-09-01
Delivery Time: 11:00:00
Location: Hyderabad

相关文章