How to store time from java.util.Date into java.sql.Date -
i want convert java.util.date
java.sql.date
want hours, minutes, , seconds java.sql.date can used store date(no time) . tried below code giving year, month, , day java.sql.date
object.
simpledateformat format = new simpledateformat("yyyymmddhhmmss"); date parsed = format.parse("20110210120534"); system.out.println(format.parse("20110210120534")); java.sql.date sql = new java.sql.date(parsed.gettime()); system.out.println("sql date is= "+sql);
current output:
2011-02-10
desired output:
2011-02-10 12:05:34
the java.sql.date
type used store date (no time) information, maps sql date
type, doesn't store time. tostring()
method is:
formats date in date escape format yyyy-mm-dd.
to achieve desired output can use java.sql.timestamp
, stores date and time information, mapping sql timestamp
type. tostring()
method outputs need:
formats timestamp in jdbc timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, ffffffffff indicates nanoseconds.
example:
java.text.dateformat format = new java.text.simpledateformat("yyyymmddhhmmss"); java.util.date date = format.parse("20110210120534"); java.sql.timestamp timestamp = new java.sql.timestamp(date.gettime()); system.out.println(timestamp); // prints "2011-02-10 12:05:34.0"
Comments
Post a Comment