Java sql.Timestamp toString() 方法示例说明


java.sql.Timestamp 类中 toString() 方法以 String 变量形式返回当前 Timestamp 对象的时间戳形式为 JDBC 转义格式。

即使用此方法,可以将 Timestamp 对象转换为 String。

//Retrieving the Time object
Timestamp timestampObj = rs.getTimestamp("DispatchTimeStamp");
//Converting the Time object to String format
String time_stamp = timestampObj.toString();

示例

让我们使用如下所示的 CREATE 语句,在 MySQL 数据库中使用名称为 dispatches_data 的表

CREATE TABLE dispatches_data(
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchTimeStamp timestamp,
   Price INT,
   Location VARCHAR(255));

现在,我们将使用 INSERT 语句在 dispatches_data 表中插入 5 条记录

insert into dispatches_data values('Key-Board', 'Raja', TIMESTAMP('2019-05-04', '15:02:45'), 7000, 'Hyderabad');
insert into dispatches_data values('Earphones', 'Roja', TIMESTAMP('2019-06-26', '14:13:12'), 2000, 'Vishakhapatnam');
insert into dispatches_data values('Mouse', 'Puja', TIMESTAMP('2019-12-07', '07:50:37'), 3000, 'Vijayawada');
insert into dispatches_data values('Mobile', 'Vanaja' , TIMESTAMP ('2018-03-21', '16:00:45'), 9000, 'Chennai');
insert into dispatches_data values('Headset', 'Jalaja' , TIMESTAMP('2018-12-30', '10:49:27'), 6000, 'Goa');

以下 JDBC 程序与数据库连接,并检索 dispatches_data 表的内容。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

public class Timestamp_toString {
   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......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Query to retrieve the contents of the dispatches_data table
      String query = "select * from dispatches_data";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
         System.out.println("Product Name: " + rs.getString("ProductName"));
         System.out.println("Customer Name: " + rs.getString("CustomerName"));
         Timestamp timeStampObj = rs.getTimestamp("DispatchTimeStamp");
         //Converting the Time object to String format
         String timeStamp = timeStampObj.toString();
         System.out.println("Dispatch time stamp in String format: " + timeStamp);
         System.out.println("Location: " + rs.getString("Location"));
         System.out.println();
      }
   }
}

在此程序中,在检索列值时,我们使用 Timestamp 类的 toString() 方法将 DeliveryTime 值从 Timestamp 对象转换为字符串格式,并试图显示它。

输出

Connection established......
Product Name: Key-Board
Customer Name: Raja
Dispatch time stamp in String format: 2019-05-04 15:02:45.0
Location: Hyderabad
Product Name: Earphones
Customer Name: Roja
Dispatch time stamp in String format: 2019-06-26 14:13:12.0
Location: Vishakhapatnam
Product Name: Mouse
Customer Name: Puja
Dispatch time stamp in String format: 2019-12-07 07:50:37.0
Location: Vijayawada
Product Name: Mobile
Customer Name: Vanaja
Dispatch time stamp in String format: 2018-03-21 16:00:45.0
Location: Chennai
Product Name: Headset
Customer Name: Jalaja
Dispatch time stamp in String format: 2018-12-30 10:49:27.0
Location: Goa

更新于:2019 年 7 月 30 日

5K+ 浏览

开启您的职业生涯

完成课程获得认证

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