我在前端的时间戳格式如下
2020-7-23 14:36:43. 132000000
我在oracle数据库中的时间戳是这样的
23-07-20 02:36:43.132000000 PM
我已经更改了时间格式,如下
private String OracleTsString(String createdDate) {
SimpleDateFormat inputFormat=null;
SimpleDateFormat outputFormat = null;
try {
inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSSSSSSSS");
Date date;
date = inputFormat.parse(createdDate);
outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss.SSSSSSSSS aa");
}
//Exception Handling
但是,当我尝试将字符串插入数据库时,我得到了ORA-01858:在预期数字的位置发现了一个非数字字符
以下是我的查询
jdbcTemplate.update(query,
editSftpBean.getSftpName(),
editSftpBean.getIp_address(),
editSftpBean.getPort_number(),
editSftpBean.getUserName(),
editSftpBean.getAuthentication(),
oracleTs//My converted string
);
插入查询::
INSERT INTO tabl_two (SFTP_NAME, IP_ADDRESS, PORT_NUMBER, USER_NAME, AUTH_TYPE,CREATED_DATE ) VALUES (?, ?, ?, ?, ?)
我该如何解决此问题?
不要使用String
将日期/时间值插入数据库,而是使用Timestamp
。
private static Timestamp parseTsString(String createdDate) {
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("uuuu-M-d H:mm:ss")
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
.toFormatter();
return Timestamp.valueOf(LocalDateTime.parse(createdDate, fmt));
}