John Topley's Knowledgebase

Retaining The Time Component When Writing To A Date Column

Saturday, 15 May 2004

Writing to a date type column in a database using the JDBC PreparedStatement's setDate method, results in the time component being lost i.e. it gets set to 00:00:00.

The code given below shows how to retain the time component. In this example, “pstmt” is a variable of type java.sql.PreparedStatement and the current date and time are used. Note that java.util.Date's getTime method can be used when specific dates need to be passed to the setTimestamp method.

pstmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));

top | index | previous | next | comments ()

home | archive | kb | media | about | contact | accessibility
Copyright © 2003 - 2005 John Topley. Made with CityDesk.