Timestamp discrepancies between SQL server and Java -


I have to repeat a simple process for the SQL server stored function from Java code. This product will go to a SQL Azure DB, but I'm testing it against my local SQL Express 12 installation.

Part of this stored procedure is to add some value to the 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_mon.base.Strings; Public Static String Context () {// init variable date format with format data date format = new SimpleDateFormat ("dd / MM / yyyy HH: mm: ss.SSS"); Timestamp date = new timestamp (date format. PRSE ("04/04/2014 21: 07: 13.897"). Match-time ()); // format variables 0-free strings string formatteddet = string Format ("% 011d", date. GetTime () / 1000); // those string string final string = ... + formatted data + ...; Return final string; }   

Variables:

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

This is equivalent to SQL:

  DECLARE @date DATETIME; DECLARE @ formatted_date CHAR (11); DECLARE @final_string CHAR (22); - Variable SAT @ date = '04/04/04 21: 07: 13.897' with the same data as Java code '; - Format Variables 0 -Free strings in SET @ formatted_date = FORMAT (DATEDIFF (s, '71 71-01-01 00:00:00', @date), '00000000000'); - Set those strings SET @final_string = CONCAT (..., @formatted_date, ...);   

Variables:

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

Checking that the output was similar, I saw that the dates are not the same:

  Java output: 01396638433 MSSQL output: 01396645633   

I opened to see what the difference is:

  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   

Exact two hours difference

I have found a query to run against SQL Server to check the time zone settings:

  DECLARE @TZ SMALLINT SELECT @ TZ = DATEPART (TZ, SYSDATETIMEOFFSET ()) DECKLARE @TimeZone VARCHAR (50) EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'System Current ControlSet \ Control \ Timezone Notice', 'Time Zonki No Name', @Timezone Select @ Time Zone, Cast ( @ TZ) / 60 VARCHAR (5)) + ':' + Cast (ABA (TAG)% 60 as VARCHAR (5));   

Output:

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

I checked this type of JVM time zone:

  calendar now = calendar.Ast Instance (); Println (now.getTimeZone ()); Println (. System.getProperties () "user.timezone" found () toString ().);   

Output:

  sun.util.calendar.ZoneInfo [id = "Europe / Berlin", offset = 3600000, dstSavings = 3600000, transition = 143 , LastRule = java.util.SimpleTimeZone [id = Europe / Berlin, offset = 3600000, dstSavings = 3600000, startYou = 0, start mode = 2, start = 2, start date = -1, startup = 1, start time = 3600000, startTimeMode = 2, Endodo = 2, Endemouth = 9, and de = -1, End of Week = 1, Endtime = 3600000, Endime Mode = 2]] Europe / Berlin   

I am Between Java How do I get timestamps and SQL Server?

On things of SQL Server -

> Getutcdate () which gives current UTC time. By comparing with getdate () , you can get the time difference between UTC and modify the value in the format.

  dated (s, getutcdate, select getdate))   

In any case better than accessing the registry.

Then, the SQL Server code should look like this:

  DECLARE @ dated DATETIME; DECLARE @ formatted_date CHAR (11); DECLARE @final_string CHAR (22); DECLARE @diff_sec int; - Variable SAT @ date = '04/04/04 21: 07: 13.897' with the same data as Java code '; - Differences in UTC and local seconds SET @diff_sec = datediff (s, getutcdate (), getdate ()); - Format Variables set in 0-free strings @ formatted_get = format (DATEDIFF (s, '1970-01-01 00:00:00', @date) - @diff_sec, '00000000000'); - Set those strings SET @final_string = CONCAT (..., @formatted_date, ...);    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -