java和sql server,SQL Server和Java之间的时间戳差异

博客作者在将Java代码中的时间处理转换为SQL Server存储过程时遇到了时间不一致的问题。Java输出的时间戳与SQL Server输出的有两小时的差异,经过调查发现是时区设置导致的。作者通过在Java应用中设置时区为UTC,并在SQL查询中使用GETUTCDATE()解决了这个问题。
摘要由CSDN通过智能技术生成

I need to replicate a simple procedure from Java code to SQL Server stored procedure. It will go into a SQL Azure db in production, but I'm testing it against my local SQL Express 12 install.

A part of this stored procedure is to concatenate some values into a string.

This is my example Java code:

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;

}

Variables:

| date | formatted_date |

| ----------------------- | -------------- |

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

This is the equivalent in 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, ...);

Variables:

| date | formatted_date |

| ----------------------- | -------------- |

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

While checking if the output was the same I noticed the dates are not the same:

Java output: 01396638433

MSSQL output: 01396645633

I opened this site to see what this difference meant:

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

Exactly two hours difference.

I've found a query to run against SQL Server to check time zone settings:

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));

Output:

| Time zone | Offset |

| ----------------------- | ------- |

| W. Europe Standard Time | 2:0 |

I checked JVM time zone like this:

Calendar now = Calendar.getInstance();

System.out.println(now.getTimeZone());

System.out.println(System.getProperties().get("user.timezone").toString());

Output:

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

How can I get equal timestamps between Java and SQL Server?

解决方案

Even though Mark Rotteveel and dean gave enlightening answers I ended up doing the following:

at the beginning of my application init method I set

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

and all SQL calls to

getdate()

have been replaced with

getutcdate()

Thank you for your time!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值