SQL Server和Java之间的时间戳差异

时间:2014-04-24 11:45:47

标签: java sql-server tsql jdbc timestamp

我需要从Java代码到SQL Server存储过程复制一个简单的过程。它将在生产中进入SQL Azure数据库,但我会根据本地SQL Express 12安装进行测试。

此存储过程的一部分是将某些值连接成一个字符串。

这是我的示例Java代码:

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.google.common.base.Strings;

public static String concat() {
  //init variables with sample data
  DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
  Timestamp date = new Timestamp(dateFormat.parse("04/04/2014 21:07:13.897").getTime());

  //format variables into 0-filled strings
  String formattedDate = String.format("%011d", date.getTime() / 1000);

  //concat those strings
  String finalString = ... + formattedDate + ...;
  return finalString;    
}

变量:

| date                    | formatted_date |
| ----------------------- | -------------- |
| 2014-04-04 21:07:13.897 | 01396638433    |

这在SQL中是等效的:

DECLARE @date DATETIME;
DECLARE @formatted_date CHAR(11);
DECLARE @final_string CHAR(22);

--init variables with same data as Java code
SET @date = '2014/04/04 21:07:13.897';

--format variables into 0-filled strings
SET @formatted_date = FORMAT(DATEDIFF(s,'1970-01-01 00:00:00', @date), '00000000000');

--concat those strings
SET @final_string = CONCAT(..., @formatted_date, ...);

变量:

| date                    | formatted_date |
| ----------------------- | -------------- |
| 2014-04-04 21:07:13.897 | 01396645633    |

在检查输出是否相同时,我注意到日期不一样:

Java output:  01396638433
MSSQL output: 01396645633

我打开this site看看这个差异意味着什么:

Java:  GMT: Fri, 04 Apr 2014 19:07:13 GMT, Your time zone: 4/4/2014 21:07:13 GMT+2
MSSQL: GMT: Fri, 04 Apr 2014 21:07:13 GMT, Your time zone: 4/4/2014 23:07:13 GMT+2

完全相差两个小时。

我找到了一个针对SQL Server运行的查询来检查时区设置:

DECLARE @TZ SMALLINT
SELECT @TZ=DATEPART(TZ, SYSDATETIMEOFFSET())

DECLARE @TimeZone VARCHAR(50)
EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\Control\TimeZoneInformation',
'TimeZoneKeyName',@TimeZone OUT

SELECT @TimeZone, CAST(@TZ/60 AS VARCHAR(5))+':'+Cast(ABS(@TZ)%60 AS VARCHAR(5));

输出:

| Time zone               | Offset  |
| ----------------------- | ------- |
| W. Europe Standard Time | 2:0     |

我像这样检查了JVM时区:

Calendar now = Calendar.getInstance();
System.out.println(now.getTimeZone());
System.out.println(System.getProperties().get("user.timezone").toString());

输出:

sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000, dstSavings=3600000,
transitions=143, lastRule=java.util.SimpleTimeZone[id=Europe/Berlin, offset=3600000, 
dstSavings=3600000, startYear=0, startMode=2, startMonth=2, startDay=-1,
startDayOfWeek=1, startTime=3600000, startTimeMode=2, endMode=2, endMonth=9,
endDay=-1, endDayOfWeek=1, endTime=3600000, endTimeMode=2]]
Europe/Berlin

如何在Java和SQL Server之间获得相同的时间戳?

3 个答案:

答案 0 :(得分:3)

在SQL Server方面 -

SQL Server中有一个名为getutcdate()的函数,它返回当前的UTC时间。将其与getdate()进行比较,您可以获得与UTC的时差,并将值修改为格式化。

select datediff(s, getutcdate(), getdate())

在任何情况下都比访问注册表更好。

因此,SQL Server代码应如下所示:

DECLARE @date DATETIME;
DECLARE @formatted_date CHAR(11);
DECLARE @final_string CHAR(22);
DECLARE @diff_sec int;

--init variables with same data as Java code
SET @date = '2014/04/04 21:07:13.897';

--get the difference between UTC and local in seconds
SET @diff_sec = datediff(s, getutcdate(), getdate());

--format variables into 0-filled strings
SET @formatted_date = FORMAT(DATEDIFF(s,'1970-01-01 00:00:00', @date) - @diff_sec, '00000000000');

--concat those strings
SET @final_string = CONCAT(..., @formatted_date, ...);

答案 1 :(得分:2)

JDBC要求 - 没有任何时区信息 - 使用本地时区存储和检索时间戳。

这意味着如果您的本地系统是Europe \ Berlin,那么在数据库中存储为2014-04-04 21:07:13.897的日期将被处理为2014-04-04 21:07:13.897 CEST(中欧夏令时),而不是2014-04-04 21:07:13.897 UTC

CEST偏移比UTC提前2小时或7200秒,这解释了您观察到的差异:

1396645633 - 1396645633 = 7200

类似,当Java将时间戳存储到数据库中时,它将发送时间戳,就像它在当前时区中一样。因此,如果您尝试存储2014-04-04 21:07:13.897 UTC,那么它会使用2014-04-04 23:07:13.897 CEST并将其发送到SQL Server 2014-04-04 23:07:13.897

虽然没有为setTimestamp(int parameterIndex, Timestamp x)明确指定,但驱动程序必须遵循setTimestamp(int parameterIndex, Timestamp x, Calendar cal) javadoc建立的规则:

  

使用给定的java.sql.Timestamp对象将指定参数设置为给定的Calendar值。驱动程序使用Calendar对象构造SQL TIMESTAMP值,然后驱动程序将其发送到数据库。使用Calendar对象,驱动程序可以计算考虑自定义时区的时间戳。 如果未指定Calendar对象,则驱动程序将使用默认时区,即运行应用程序的虚拟机的时区。

有关详情,请参阅:Is java.sql.Timestamp timezone specific?

答案 2 :(得分:1)

尽管Mark Rotteveel和院长给出了启发性答案,但我最终还是做了以下事情:

在我的应用程序初始化方法我设置

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

以及对

的所有SQL调用
getdate()

已替换为

getutcdate()

感谢您的时间!