Java 错误时间戳格式必须是 yyyy-mm-dd hh:mm:ss[.fffffffff] 同时将时间戳保存到数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26344819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:18:31  来源:igfitidea点击:

Error Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff] while saving timestamp into databse

javadatabasecalendartimestampsimpledateformat

提问by tom

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss a z");
sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
String DateToStoreInDataBase= sdf.format(obj1.getSomeDate().toGregorianCalendar().getTime());
System.out.println(emprSubDte);

Timestamp ts = Timestamp.valueOf(emprSubDte);

preparedStatement.setTimestamp(72,ts);

sysout of DateToStoreInDataBase is = " 2014-19-13 12:19:59 PM EDT" when i am trying to save this result into database in am getting error Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff].

sysout of DateToStoreInDataBase is = " 2014-19-13 12:19:59 PM EDT" 当我试图将此结果保存到数据库中时出现错误时间戳格式必须为 yyyy-mm-dd hh:mm:ss[.ffffffffff ]。

I have the same format but still i am reciving the error.

我有相同的格式,但我仍然收到错误。

回答by June

The problem is in your SimpleDateFormat instantiation. You're using this pattern: "yyyy-mm-dd hh:mm:ss a z"

问题在于您的 SimpleDateFormat 实例化。您正在使用此模式:“yyyy-mm-dd hh:mm:ss a z”

The issue is in the month. It should be "MM" instead of "mm". "mm" is for "minute in the hour".

问题是在这个月。它应该是“MM”而不是“mm”。“mm”表示“一小时内的分钟”。

Reference: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

参考:http: //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

回答by Daniel Adenew

check if there is incompatible setter or getter, either form your database model side , view pages , beans to xhtml mapping. I found date value for float and fixed it.

检查是否存在不兼容的 setter 或 getter,无论是形成您的数据库模型端、查看页面、bean 到 xhtml 的映射。我找到了 float 的日期值并修复了它。