SQL server和Java之间的时间戳差异
我需要将一个简单的过程从Java代码复制到SQL Server存储过程。它将进入生产中的SQLAzure数据库,但我正在针对本地SQLExpress 12安装进行测试 此存储过程的一部分是将一些值连接到字符串中 这是我的示例Java代码:SQL server和Java之间的时间戳差异,java,sql-server,tsql,jdbc,timestamp,Java,Sql Server,Tsql,Jdbc,Timestamp,我需要将一个简单的过程从Java代码复制到SQL Server存储过程。它将进入生产中的SQLAzure数据库,但我正在针对本地SQLExpress 12安装进行测试 此存储过程的一部分是将一些值连接到字符串中 这是我的示例Java代码: import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; impor
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
我打开门来看看这种差异意味着什么:
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 |
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
我像这样检查JVM时区:
Calendar now = Calendar.getInstance();
System.out.println(now.getTimeZone());
System.out.println(System.getProperties().get("user.timezone").toString());
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, ...);
输出:
| Time zone | Offset |
| ----------------------- | ------- |
| W. Europe Standard Time | 2:0 |
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之间获得相等的时间戳?JDBC要求(没有任何时区信息)使用本地时区存储和检索时间戳 这意味着,如果您的本地系统是欧洲/柏林,则数据库中存储为
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 Server2014-04-04 23:07:13.897
虽然没有为setTimestamp(int-parameterIndex,Timestamp x)明确指定它,
驱动程序必须遵循以下规则:
使用给定的Calendar
对象,将指定参数设置为给定的java.sql.Timestamp
值。驱动程序使用日历
对象构造一个SQL时间戳
值,然后驱动程序将该值发送到数据库。使用日历
对象,驱动程序可以根据自定义时区计算时间戳如果未指定任何日历
对象,则驱动程序将使用默认时区,即运行应用程序的虚拟机的时区。
有关更多详细信息,请参见:关于SQL Server方面的内容-- SQL Server中有一个名为
getutcdate()
的函数,它返回当前UTC时间。将其与getdate()
进行比较,可以从UTC获取时差并将值修改为格式
select datediff(s, getutcdate(), getdate())
在任何情况下都比访问注册表好
因此,SQL Server代码应该如下所示:
Calendar now = Calendar.getInstance();
System.out.println(now.getTimeZone());
System.out.println(System.getProperties().get("user.timezone").toString());
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, ...);
尽管马克·罗特维尔和迪恩给出了启发性的答案,但我最终还是做了以下几点: 在应用程序开始时,我设置了init方法
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
以及所有对
getdate()
已被替换为
getutcdate()
谢谢你抽出时间 JDBC中的时间戳是使用本地时区所必需的,请参阅感谢您的明确回答。我不清楚的是如何更改Java代码以使事情正确。我当前使用新的时间戳(System.currentTimeMillis())生成时间戳,然后将该值发送到数据库。稍后,会有一个计划的SQL存储过程来读取该值并进行一些计算。你能建议我做些什么改变吗?如果你想总是使用一个特定的时区,那么就使用setter(和getter)方法,它接受一个
日历
对象;并使用带有正确时区的日历。如果使用java 8,并且您的驱动程序已经实现了JDBC 4.2中指定的新的<代码> java .Time< /Cuff>支持,您也可以考虑使用<代码> java .Time.LoalDATECTIMECT/<代码>类,连同<代码> SET/GETObjult<代码>(但我不确定有多少驱动程序已经完全支持这个)。因为如果我将服务器移动到另一个区域,会导致转换问题。。是否有办法始终将时间戳保存/读取为UTC?是:在设置或检索时间戳时,使用时区设置为UTC的日历
,或者使用LocalDateTime
对象(如果您的驱动程序支持)。谢谢您的回答。所以你们认为问题在于SQL server端?getutcdate()应该如何帮助我解决字符串格式问题?我应该如何处理它的7200输出?