如何在Java中使用LocalDateTime類在表中設定本地日期/時間?


Java8的java.time包提供了一個名為LocalDateTime的類,用於獲取本地日期和時間的當前值。除了日期和時間值之外,您還可以獲取其他日期和時間欄位,例如一年中的某一天、一週中的某一天和一年中的某一週。

將本地時間設定為列

要將本地日期和時間值設定為表中的列:

  • 獲取LocalDateTime物件 - 您可以透過呼叫靜態方法now()來獲取LocalDateTime物件,如下所示:
//Getting the LocalDateTime object
LocalDateTime localDateTime = LocalDateTime.now();
  • 從上面獲得的LocalDateTime中獲取LocalDateLocalTime物件,如下所示:
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime()
  • 現在,將LocalDateLocalTime物件分別傳遞到java.sql.Datejava.sql.Time類的valueOf()方法,如下所示:
java.sql.Date date = java.sql.Date.valueOf(localDate);
java.sql.Time time = java.sql.Time.valueOf(localTime);

示例

讓我們使用CREATE語句在MySQL資料庫中建立一個名為dispatches的表,如下所示:

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

現在,我們將使用INSERT語句在dispatches表中插入5條記錄:

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');
insert into dispatches values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into dispatches values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into dispatches values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

以下JDBC程式透過傳遞所需的值將新記錄插入到dispatches表中。在這裡,我們獲取當前本地日期和時間值,並將它們作為表中日期和時間列的值插入。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
public class settingLocatDate {
   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......");
      //Getting the LocalDateTime object
      LocalDateTime localDateTime = LocalDateTime.now();
      System.out.println(localDateTime.toString());
      //Converting date and time values from local to SQL
      java.sql.Date date = java.sql.Date.valueOf(localDateTime.toLocalDate());
      java.sql.Time time = java.sql.Time.valueOf(localDateTime.toLocalTime());
      //Creating a Prepared Statement
      String query = "INSERT INTO Dispatches VALUES (?, ?, ?, ?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Watch");
      pstmt.setString(2, "Rajan");
      pstmt.setDate(3, date);
      pstmt.setObject(4, time);
      pstmt.setInt(5, 4000);
      pstmt.setString(6, "Chennai");
      pstmt.execute();
      System.out.println("Rows inserted ....");
      //Retrieving values
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatches");
      while(rs.next()) {
         System.out.println("Product Name: "+rs.getString("ProductName"));
         System.out.println("Customer Name: "+rs.getString("CustomerName"));
         System.out.println("Date Of Dispatch: "+rs.getDate("DispatchDate"));
         System.out.println("Delivery Time: "+rs.getTime("DeliveryTime"));
         System.out.println("Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

輸出

Connection established......
2019-05-14T15:48:42.457
Rows inserted ....
Product Name: Key-Board
Customer Name: Raja
Date Of Dispatch: 2019-09-01
Delivery Time: 11:00:00
Location: Hyderabad
Product Name: Earphones
Customer Name: Roja
Date Of Dispatch: 2019-05-01
Delivery Time: 11:00:00
Location: Vishakhapatnam
Product Name: Mouse
Customer Name: Puja
Date Of Dispatch: 2019-03-01
Delivery Time: 10:59:59
Location: Vijayawada
Product Name: Mobile
Customer Name: Vanaja
Date Of Dispatch: 2019-03-01
Delivery Time: 10:10:52
Location: Chennai
Product Name: Headset
Customer Name: Jalaja
Date Of Dispatch: 2019-04-06
Delivery Time: 11:08:59
Location: Goa
Product Name: Watch
Customer Name: Rajan
Date Of Dispatch: 2019-05-14
Delivery Time: 15:48:42
Location: Chennai

更新於:2019年7月30日

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告