Ashish
Ashish

Reputation: 14707

How to convert Date and Time to timeStamp

How can I convert date in MM/DD/YYYY format and time in hh:mm:ss to Timestamp so that I can save it in hsql database.

Upvotes: 2

Views: 389

Answers (1)

Marc
Marc

Reputation: 1820

java.sql.Timestamp has a constructor that takes a long, so first we parse your date string using a simple date format then get the time of date (long) and pass it as the argument for the Timestamp contructor.

Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(yourDateString)
long time = date.getTime();
Timestamp timestamp = new Timestamp(time);

You could also do the following:

Timestamp timestamp = Timestamp.valueOf("2014-08-18 8:19:15.0");

Upvotes: 1

Related Questions