Convert string to java.sql.Timestamp

Similar to converting a string to a date, this example will show how to convert a string into a java.sql.Timestamp. First using SimpleDateFormat we will establish a pattern which we expect the string to be in when we parse it. Next calling the SimpleDateFormat.parse we will convert the string into a java.util.Date. Passing the milliseconds to a constructor of Timestamp we will initialize a new instance.

Straight up Java

@Test
public void parse_string_timestamp_java() throws ParseException {

    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd hh:mm:ss:SSS");

    Date parsedTimeStamp = dateFormat.parse("2014-08-22 15:02:51:580");

    Timestamp timestamp = new Timestamp(parsedTimeStamp.getTime());

    assertEquals(1408737771580l, timestamp.getTime());
}