Wednesday, April 20, 2011

Get SQL Type Timestamp from string type date

Generally we use a sysdate to insert the timestamp in table in Oracle DB. But sometime we need to manage at the java side, then need to convert the String type date to java.sql.Timestamp.
In this case following utiltiy may be useful.

To call following method,
e.g.

java.sql.Timestamp sqlTimestamp = getStringToSqlTimeStamp(”12-Jun-2010″);

public static java.sql.Timestamp getStringToSqlTimeStamp(String strDate) throws Exception
{
final SimpleDateFormat simpleDateFmt = new SimpleDateFormat(”dd-MMM-yyyy”);
Date date = simpleDateFmt.parse( strDate );
java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());

return timeStampDate ;
}

No comments: