Handling Date and Timestamp Values

This section describes how to handle Date and Timestamp Values.

When you are dealing with Date or Timestamp values in database, you should use the data type classes provided in the java.sql.* package, not the java.lang.* package. java.sql.* classes are better mapped to database data types. For example, java.sql.Date class is mapped to the database DATE data type better than java.lang.Date class. The toString() method of java.sql.Date class will format a Date object to the format yyyy-mm-dd supported by most database servers.

The following sample Java program shows you how to use java.sql.Date and java.sql.Timestamp classes to handle date and timestamp values in SQL statements and result sets:

/* DerbyInsertDates.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class DerbyInsertDates {
  public static void main(String [] args) {
    Connection con = null;
    try {
      con = DriverManager.getConnection("jdbc:derby://localhost/TestDB");
      Statement sta = con.createStatement();

// Converting Java Date to SQL TIMESTAMP
      int count = 0;
      java.sql.Date birthDate = java.sql.Date.valueOf("2010-02-29");
      java.sql.Timestamp now = new java.sql.Timestamp(
        System.currentTimeMillis());
      count += sta.executeUpdate(
        "INSERT INTO Profile"
        + " (FirstName, LastName, BirthDate, ModTime)"
        + " VALUES ('Keith', 'Harris', '"+birthDate+"', '"+now+"')");
      System.out.println("Number of rows inserted: "+count);

// Checking inserted rows
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Profile");
      System.out.println("List of Profiles: ");
      while (res.next()) {
         System.out.println(
           "  "+res.getInt("ID")
           + ", "+res.getString("FirstName")
           + ", "+res.getString("LastName")
           + ", "+res.getFloat("Point")
           + ", "+res.getDate("BirthDate")
           + ", "+res.getTimestamp("ModTime"));
      }
      res.close();

      sta.close();
      con.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

Noticed that I used res.getTimestamp() instead of res.getDate() to retrieve time and millisecond information:

Number of rows inserted: 1
List of Profiles:

 1, Herong, null, 0.0, 1999-12-31, 2016-12-31 23:59:59.999
 2, Janet, Gates, 999.99, 2004-10-13, 2016-12-31 23:59:59.999
 3, 1403, 896f7, 62.594772, 1999-12-31, 2016-12-31 23:59:59.999
 4, 256d, aa3f5, 203.51392, 1999-12-31, 2016-12-31 23:59:59.999
 5, d7a, 3a06f, 604.9995, 1999-12-31, 2016-12-31 23:59:59.999
 6, 8ef, 466ca, 31.71128, 1999-12-31, 2016-12-31 23:59:59.999
 7, 1a34, 59e87, 674.8019, 1999-12-31, 2016-12-31 23:59:59.999
 8, 10cc, ab361, 325.30505, 1999-12-31, 2016-12-31 23:59:59.999
 9, 11bd, 99552, 315.1111, 1999-12-31, 2016-12-31 23:59:59.999
 10, 8c4, 68909, 23.095905, 1999-12-31, 2016-12-31 23:59:59.999
 11, 17e1, 99a5d, 186.09494, 1999-12-31, 2016-12-31 23:59:59.999
 12, 1ea2, 480c7, 131.6408, 1999-12-31, 2016-12-31 23:59:59.999
 13, Keith, Harris, 0.0, 2010-03-01, 2020-02-23 15:12:36.431

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

Java DB (Derby) - DML Statements

 Tables with Primary Key Column "GENERATED ... AS IDENTITY"

 "INSERT INTO" Statements

 "INSERT INTO" Statements with INDENTITY Columns

Handling Date and Timestamp Values

 "UPDATE" Statements

 "DELETE FROM" Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB