Java将字符串解析为日期返回错误的日期 [英] Java parsing String to a Date returns incorrect Date
问题描述
我将2种Java日期类型存储为MySQL数据库表的日期和时间.我正在使用SimepleDateFormat("YYYY-MM-dd")
将日期存储在数据库中,当我选择它时它显示为正确的日期.但是,当我尝试将其解析回util.Date并创建新的事件对象时,它显示为30/12/2012
而不是数据库中的31/05/2013
.解析为util.Date并格式化后的时间可以正确打印.我不确定日期为什么打印错误的日期,但是时间却打印正确的时间.
I am storing my 2 Java date types as Date and Time for a MySQL database table. I am using the SimepleDateFormat("YYYY-MM-dd")
to store the date in my database and it shows up as the correct date when i go to select it. However when i try to parse it back into a util.Date and create a new Event Object, it shows up as 30/12/2012
instead of 31/05/2013
as it is in the database. The time, when parsed into a util.Date and formatted prints out correctly. I am not sure why the Date is printing the wrong date, but the time is printing the correct time.
数据库
+--------+--------------+-----------+
+ EVENT1 + 2013-05-31 + 02:30:00 +
+--------+--------------+-----------+
+ EVENT2 + 2013-05-31 + 01:00:00 +
+--------+--------------+-----------+
打印:
Event1
30/12/2012
02:30
Event2
30/12/2012
01:00
推荐答案
应为yyyy-MM-dd
,且小写字母Ys.请参阅此处以了解大写字母Y的含义...
It should be yyyy-MM-dd
with lower case Ys. See here for what the capital Y means...
Y返回2012,而y返回2011的SimpleDateFormat
这篇关于Java将字符串解析为日期返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!