java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String tm = "2019-02-09 07:35:54";
LocalDateTime dateTime = LocalDateTime.parse(tm, formatter);
System.out.println(dateTime);
This produces the following output:
2019-02-09T07:35:54
I discourage the use of SimpleDateFormat, Date and Timestamp. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead I am using java.time, the modern Java date and time API.
JDBC 4.2+
If you thought you needed a Timestamp for your database, you probably don’t. Assuming your JDBC driver is up to JDBC 4.2 you can directly give it the LocalDateTime object we just produced. For example like:
PreparedStatement ps = yourConnection.prepareStatement(
"insert into your_table(your_timestamp_col) values (?);");
ps.setObject(1, dateTime);
What went wrong in your code?
There are two errors in your format pattern string, yyyy-MM-dd hh:mm:ss.SSS:
- Assuming that the hours in the user input are hour of day from 00 through 23, you need uppercase
HH to parse them, as LppEdd already said in a comment (lowercase hh is for hour within AM or PM from 01 through 12 and of course requires that an AM or PM marker is present).
- Since the user input doesn’t include fraction of second, you should not have
.SSS in the format pattern (this is probably what caused your exception).
Link
Oracle tutorial: Date Time explaining how to use java.time.
tmvariable? Perhaps it doesn't match your format.