日期一贯两天 [英] dates consistently two days off
问题描述
我使用Java JDBC将日期写入sql server 2008,然后读回来。
读取的日期始终比写入日期早两天。
我将包含Date字段的行插入一个准备的语句。日期值由以下提供:
java.sql.Date todaysDate = new java.sql.Date(System.currentTimeMillis() );
System.out.println(todaysDate.toString()) - > 2012-07-02
ps.setDate(8,todaysDate);
在将数据写入数据库后,如果我运行的话,sql server会显示正确的日期: p>
从table_name中选择日期date!= null - > 2012-07-02
如果我通过JDBC运行相同的查询,然后使用
<$ p从结果集中检索日期值$ p>
java.sql.Date sqlDate = rs.getDate(date);
sqlDate.toString() - > 2012-06-30
插入的行是表中唯一没有空值的行,所以这看起来不是读错误的记录。
我以为这是一个众所周知的问题,但Google搜索唯一的两天问题的唯一参考依据是没有明确的答案。
任何想法?
beeky(生活在过去)
JDBC驱动程序错误
原来,问题是MS jdbc驱动程序。我尝试了所有可能的日期类型和日期转换的组合,没有任何工作。经过大量的搜索(应该做到这一点!),我看到一个旧的SO条目的意见,暗示问题是从Microsoft的版本3 jdbc驱动程序。我得到最新的驱动,版本4.something,问题消失了。
感谢所有试图帮助的人。特别感谢你Mike花时间发布解决方案。
- = beeky
I am using Java JDBC to write a date to sql server 2008 and then read it back.
The date as read is consistently two days earlier than the date as written.
I am inserting the row containing the Date field with a prepared statement. The date value is provided by:
java.sql.Date todaysDate = new java.sql.Date(System.currentTimeMillis()) ;
System.out.println(todaysDate.toString()) -> 2012-07-02
ps.setDate(8, todaysDate);
After writing the date to the db, sql server shows me the correct date if I run:
select date from table_name where date!=null ->2012-07-02
If I run the same query via JDBC then retrieve the date value from the resultset using
java.sql.Date sqlDate = rs.getDate("date") ;
sqlDate.toString() ->2012-06-30
The inserted row is the only row in the table with a non-null date so this does not appear to be a case of reading the wrong record.
I thought this would be a well known problem but the only reference I could find by a Google search for a "two days off" issue had no definitive answer.
Any ideas?
beeky (living in the past)
Faulty JDBC Driver
It turns out the problem was the MS jdbc driver. I tried every possible combination of date types and date conversions and nothing worked. After a great deal of searching (should have done that first!) I saw a comment on an older SO entry that implied the problem was the version 3 jdbc driver from Microsoft. I got the latest driver, version 4.something, and the problem went away.
Thanks to all that tried to help. Special thanks to you Mike for taking the time to post a solution. -=beeky
这篇关于日期一贯两天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!