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

标签 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 之间获得相同的时间戳?

最佳答案

在 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, ...);

关于java - SQL Server 和 Java 之间的时间戳差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23267971/

相关文章:

c# - 需要帮助以使用交易范围

sql - 加快临时表更新的性能

java - Android连接sql数据库

java - 应用程序内通信(Silverlight 到 Java)

java - 如何获取 "dd"标签的文本?

SQL 服务器 2008 : Insert variable into DML statements using Stored Procedure

sql - 忽略负号 SQL SERVER

java - PropertyPlaceholderConfigurer : can i have a dynamic location value

TSQL:如何在没有游标的情况下使用生成的 sql 命令字符串填充变量

mysql - 查找第一次出现的连续开始/结束列