Get LocalDateTime Object from Java SQL Date using JDBC



The java.time package of Java8 provides a class named LocalDateTime is used to get the current value of local date and time. Using this in addition to date and time values you can also get other date and time fields, such as day-of-year, day-of-week and week-of-year.

Converting java.sql.Date to LocalDateTime

The java.sql.TimeStamp class provides a method with name toLocalDateTime() this method converts the current timestamp object to a LocalDateTime object and returns it.

To convert date to LocalDateTime object.

  • Create a Timestamp object from Date object using the getTime() method as −
Date date = rs.getDate("DispatchDate");
//Converting Date to Timestamp
Timestamp timestamp = new Timestamp(date.getTime());
  • Now, convert the Timestamp object to LocalDateTime object using the toLocalDateTime() method.
Time time = rs.getTime("DeliveryTime");
//Converting time to Timestamp
Timestamp timestamp = new Timestamp(time.getTime());
//Time stamp to LocalDateTime
timestamp.toLocalDateTime();

Example

Let us create a table with name dispatches in MySQL database using CREATE statement as follows −

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

Now, we will insert 2 records in dispatches table using INSERT statements −

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');

Following JDBC program establishes connection with the database and retrieves the contents of the dispatches_data table and, converts the Date value (Dispatch_Date column) of the first record to LocalDateTime object and displays it along with its contents.

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://localhost/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();
   }
}

Output

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
Updated on: 2019-07-30T22:30:26+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements