【发布时间】:2017-04-28 00:15:18
【问题描述】:
我正在尝试将日期和时间保存在 datetime 类型 ('0000-00-00 00:00') 的属性中。我使用了以下代码。但是错误 - HTTP Status 500 - Internal Error 出现,因为 line 6 显示以下错误消息:
java.time.format.DateTimeParseException: Text '2017-04-30 23:59 could not be parsed at index 10
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm");
LocalDateTime forDate = LocalDateTime.parse(newDate, formatter);
out.println(forDate);
ps = con.prepareStatement("INSERT INTO reminder_logs VALUES(NULL, ?, ?)");
ps.setInt(1, r_id);
ps.setDate(2, forDate);
i = ps.executeUpdate();
我尝试使用setTimestamp(2, forDate) 而不是setDate(2, forDate),但随后出现错误提示 - 不兼容的类型:LocalDateTime 无法转换为 Timestamp。
我尝试从以下链接中获取参考,但没有任何帮助:
我可以做些什么来解决这个错误? 我正在运行 java se 8。
【问题讨论】:
-
afaik,格式为
yyyy-MM-dd HH:mm:ss -
但我不想要秒,我该怎么办?
-
加
:00? -
但是
newDate来自另一个yyyy-MM-dd HH:mm格式的 jsp 页面.. -
MySQL datetime 字段包含秒数,所以按照@tadman 的建议进行操作,并将
:00附加到您的格式字符串中。