如何使用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.