如何在 mysql 查询中插入日期?
how can I insert date in mysql query?
我正在尝试在 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();
已编辑: 错误 - HTTP 状态 500 - 内部错误
我尝试使用 setTimestamp(2, forDate)
而不是 setDate(2, forDate)
,但随后我收到一条错误消息 - 不兼容的类型:LocalDateTime
无法转换为 Timestamp
。
我尝试从以下链接中获取参考,但其中 none 帮助了我:
How to set current date and time using prepared statement?
我该怎么做才能解决这个错误?
我是运行javase 8.
java.time.format.DateTimeParseException
的原因是您在格式字符串中使用 hh
而不是 HH
。
H hour-of-day (0-23) number 0
h clock-hour-of-am-pm (1-12) number 12
然后你给出一个 24 小时格式的时间戳。
// runnable example
String newDate = "2017-04-30 23:59";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm");
LocalDateTime forDate = LocalDateTime.parse(newDate, formatter);
System.out.println(forDate);
结果:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-04-30 23:59' could not be parsed: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 23
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at _Scratchpad.SO.main(SO.java:12)
Caused by: java.time.DateTimeException: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 23
....
这条轨迹清楚地表明时间超出范围:
Invalid value for ClockHourOfAmPm (valid values 1 - 12): 23
我正在尝试在 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();
已编辑: 错误 - HTTP 状态 500 - 内部错误
我尝试使用 setTimestamp(2, forDate)
而不是 setDate(2, forDate)
,但随后我收到一条错误消息 - 不兼容的类型:LocalDateTime
无法转换为 Timestamp
。
我尝试从以下链接中获取参考,但其中 none 帮助了我:
How to set current date and time using prepared statement?
我该怎么做才能解决这个错误? 我是运行javase 8.
java.time.format.DateTimeParseException
的原因是您在格式字符串中使用 hh
而不是 HH
。
H hour-of-day (0-23) number 0
h clock-hour-of-am-pm (1-12) number 12
然后你给出一个 24 小时格式的时间戳。
// runnable example
String newDate = "2017-04-30 23:59";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm");
LocalDateTime forDate = LocalDateTime.parse(newDate, formatter);
System.out.println(forDate);
结果:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-04-30 23:59' could not be parsed: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 23
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at _Scratchpad.SO.main(SO.java:12)
Caused by: java.time.DateTimeException: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 23
....
这条轨迹清楚地表明时间超出范围:
Invalid value for ClockHourOfAmPm (valid values 1 - 12): 23