简体   繁体   中英

Convert Joda Date Time String To Java.Sql.TimeStamp for an INSERT

Here is my string: 2016-07-29T17:15:46.838Z
I want to insert this into a MySQL DATETIME(6) column.

Here is the method I created to convert the string to a java.sql.Timestamp

private java.sql.Timestamp convertToJavaSqlTimeStamp(String p_dateTimeString) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss:SSS");
java.sql.Timestamp _timeStamp = new java.sql.Timestamp(formatter.parseDateTime(p_dateTimeString).getMillis());
return _timeStamp;
}

.
.
.

SQL Insert logic...

PreparedStatement preparedStatement ;
preparedStatement  = _mysqlConn.prepareStatement("INSERT INTO myTable (my_date_time) VALUES (?)");
preparedStatement.setTimestamp(1, convertJodaDateTimeStringToJavaSqlTimeStamp("2016-07-29T17:15:46.838Z"));
preparedStatement.executeUpdate();

Error Message: Invalid format: "2016-07-29T17:15:46.432Z" is malformed at "-07-29T17:15:46.432Z"

Going off Uueerdo's comment. The following did the trick

DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

Thank you sir.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM