Here is a code sample to illustrate the problem.
First prep the table:
update VOLSS_customer_persons
set vcp_last_pw_change = 'Jan 1, 1970 00:00:00.990', vcp_last_login = 'Jan
1, 1970 00:00:00.987'
where vcp_id = 22624
/** These are the classes I'm using **/
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
Here's the code. I'm running it as a JUnit test case.
public void testSomething() throws Exception {
/**
* get a connection to your db first
**/
Connection con = db.getConnection();
// everything below here can be used verbatim
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select vcp_last_login as milli987,
vcp_last_pw_change as milli990 from VOLSS_customer_persons where vcp_id =
22624");
Timestamp milli987 = null;
Timestamp milli990 = null;
while (rs.next()) {
milli987 = rs.getTimestamp("milli987");
milli990 = rs.getTimestamp("milli990");
}
rs.close();
stmt.close();
con.close();
// should end with .987 and does
System.out.println("milli987 = " + milli987.toString());
// should end with.990, but instead shows .99
System.out.println("milli990 = " + milli990.toString());
/**
* Here's where the bug can get you into trouble
*/
String DB_TIMESTAMP_FORMAT = "yyyy-MM-dd kk:mm:ss.SSS";
SimpleDateFormat dateParser = new
SimpleDateFormat(DB_TIMESTAMP_FORMAT);
Date dtMilli987 = dateParser.parse(milli987.toString());
Date dtMilli990 = dateParser.parse(milli990.toString());
System.out.println("dtMilli987 = " + dateParser.format(dtMilli987));
System.out.println("dtMilli990 = " + dateParser.format(dtMilli990));
/**
* Jan 1, 1970 00:00:00.990 should be after Jan 1, 1970
00:00:00.987, but the assertion
* fails because the trailing 0 was removed from 990
*/
assertTrue(dtMilli990.after(dtMilli987));
}
>> Stay informed about: ResultSet.getTimestamp() truncating trailing zeros in mill..