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


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

将java.sql.Date转换为LocalDateTime

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

将日期转换为LocalDateTime对象。

  • 使用getTime()方法从Date对象创建一个Timestamp对象,如下所示:
Date date = rs.getDate("DispatchDate");
//Converting Date to Timestamp
Timestamp timestamp = new Timestamp(date.getTime());
  • 现在,使用toLocalDateTime()方法将Timestamp对象转换为LocalDateTime对象。
Time time = rs.getTime("DeliveryTime");
//Converting time to Timestamp
Timestamp timestamp = new Timestamp(time.getTime());
//Time stamp to 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表中插入两条记录:

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表的內容,并将第一条记录的Date值(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 {
      //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......");
      //Retrieving values
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatches");
      rs.next();
      //Retrieving the Date from the table
      Date date = rs.getDate("DispatchDate");
      //Converting Date to Timestamp
      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

更新于:2019年7月30日

4K+ 次浏览

启动你的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.