The Answer by a_horse_with_no_name is correct. Here's a bit more detail and example.
Your input string fails to comply with both:
- SQL format: YYYY-MM-DD HH:MM:SS
- ISO 8601 format: YYYY-MM-DDTHH:MM:SS (with a
T separating date from time)
In contrast, your input string has a hyphen in the middle, and fails to use colon characters for delimiting hours-minutes-seconds.
The ISO 8601 formats are used by default when the java.time are parsing or generating strings.
String input = "2020-05-07T10:05:05.301011" ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
For working in Java, you should be passing objects as values for the placeholders in a prepared statement rather than as text within a String object.
String sql = "INSERT INTO some_table ( some_timestamp ) VALUES ( ? ) ;" ;
myPreparedStatement.setObject( 1 , ldt ) ;
If you insist on using straight text rather than objects, use SQL formatting for the date-time input. I would simply take the ISO 8601 output generated by default in java.time and replace the T with a SPACE.
String sql = String sql = "INSERT INTO some_table ( some_timestamp ) VALUES ( " + ldt.toString().replace( "T" , " " ) + " ) ;" ;
Alternatively, you could generate that text by using a DateTimeFormatter object. Search Stack Overflow as that has been covered many many times already.
Moment
You have another crucial problem in your code. Your use of the word "timestamp" suggest you are trying to track a moment, a specific point on the timeline. Your input string fails to do that. Your input lacks the context of an offset-from-UTC (a number of hours-minutes-second ahead/behind the prime meridian) or a time zone (a name in Continent/Region format).
Take for example your input of 10 AM. We have no way to know if you meant 10 AM in Tokyo Japan or 10 AM in Toledo Ohio US, two very different moments many hours apart.
If you are tracking moments, you must use:
- A database column of a type akin to the SQL standard
TIMESTAMP WITH TIME ZONE.
- One of these Java classes:
Instant, OffsetDateTime, or ZonedDateTime (with only OffsetDateTime required to be supported in JDBC 4.2).
Example:
OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC ) ; // Capture the current moment in UTC.
To represent a moment as text, you should use ISO 8601 format ending in either Z (for UTC, an offset of zero), an offset such as +05:30, or an offset followed by the name of a time zone in square brackets. That last one is a non-standard extension to ISO 8601 used by the java.time.ZonedDateTime class.
Instant instant = Instant.parse( "2020-05-07T10:05:05.301011Z" ) ;
JDBC 4.2 requires support for OffsetDateTime but not Instant. Your JDBC driver may support Instant, but if not, convert.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
When tracking moments, never use a database type akin to the SQL standard type TIMESTAMP WITHOUT TIME ZONE. And never use the Java class LocalDateTime for moments. Both intentionally lack the context of a time zone or offset-from-UTC.
