if(sqlData.get(i) != null){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.sql.Timestamp myDate = new java.sql.Timestamp(dateFormat.parse(sqlData.get(i)).getTime());
preparedStatement.setTimestamp(i+1, myDate);
我不确定我做错了什么,我不断在第 3 行收到“java.text.ParseException:无法解析的日期:”。
这些是我从数据库中提取的日期,并且设置在过去。
System.out.println(sqlData.get(i));
打印出来:2014-04-19 05:48:22
最佳答案
java.text.ParseException: Unparseable date
当传递给它的字符串与指定模式的格式不同时,从 dateFormat.parse(
中抛出。
另请注意,您没有发布完整的异常消息(整行)。本来是
java.text.ParseException: Unparseable date:2014-04-19 05:48:22
在你的情况下
2014-04-19 05:48:22
不是格式 "yyyy/MM/dd HH:mm:ss"
你的模式字符串应该是
yyyy-MM-dd HH:mm:ss Note the - instead of /
关于java - 如何将过去的日期作为日期时间插入 MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24314592/