Java sql.Time setTime() method with example

The setTime() method of the java.util.Time class accepts a variable of long type, representing the number of milliseconds from the epoch time (January 1, 1970 00:00:00.000 GMT) to the required time, and sets the specified time value to the current Time object
//Setting time
time.setTime(time_value_in_long);
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 5 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');
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');
Following JDBC example inserts a new record into the dispatches table by passing the required values.
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
public class Time_setTime {
   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......");
      //Instantiating the Time class
      Time time = new Time(0L);
      //Setting time
      time.setTime(new java.util.Date().getTime());
      //Date object
      Date date = new Date(time.getTime());
      //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.setTime(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();
      }
   }
}
Here, in this program we have instantiated a Time class by passing 0L to its constructor (epoch time:1970-01-01 05:30:00.0) and changed the time to current time using the setTime() method.
And we have created a Date object by passing the time value of the Time object created above and inserted these time and date values as values of the new record.
Connection established......
Rows inserted ....
Product Name: Key-Board
Customer Name: Raja
Date of Dispatch: 1970-01-19
Delivery Time: 08:51:36
Location: Hyderabad
Product Name: Earphones
Customer Name: Roja
Date of Dispatch: 1970-01-19
Delivery Time: 05:54:28
Location: Vishakhapatnam
Product Name: Mouse
Customer Name: Puja
Date of Dispatch: 1970-01-19
Delivery Time: 04:26:38
Location: Vijayawada
Product Name: Mobile
Customer Name: Vanaja
Date of Dispatch: 1970-01-19
Delivery Time: 04:26:35
Location: Chennai
Product Name: Headset
Customer Name: Jalaja
Date of Dispatch: 1970-01-19
Delivery Time: 05:19:16
Location: Delhi
Product Name: Watch
Customer Name: Rajan
Date Of Dispatch: 2019-03-28
Delivery Time: 13:08:04
Location: Chennai