Ramblings of a DBA

Tara Kizer
posts - 165, comments - 832, trackbacks - 75

My Links

Advertisement

News

Subscribe
Search this Blog

Archives

Post Categories

Work

Convert from/to Java time

I recently had to create a UDF that would convert a Java time value to a datetime value.  We have an application that stores dates and times as the number of milliseconds since Jan. 1, 1970.  Well that isn't very pleasant to the human eye when trying to debug things.  So, here is a function to convert it to datetime:


CREATE FUNCTION udf_ConvertJavaTimeToDateTime
(@milliseconds BIGINT)
RETURNS DATETIME
AS

BEGIN

DECLARE @DateTime DATETIME

SELECT @DateTime = DATEADD(s, @milliseconds/1000, '1970-01-01 00:00:00.000' )

RETURN @DateTime

END

 

And here is a function to convert a datetime value to Java time:

CREATE  FUNCTION udf_ConvertDateTimeToMSSince1970
(@DateTime DATETIME)
RETURNS BIGINT
AS

BEGIN

DECLARE @seconds BIGINT

SELECT @seconds = DATEDIFF(s, '1970-01-01 00:00:00.000', @DateTime)

RETURN @seconds * 1000 --converts it to milliseconds

END

Print | posted on Monday, November 10, 2003 4:16 PM | Filed Under [ SQL Server - General ]

Feedback

Gravatar

# re: Convert from/to Java time

thank you, this came in handy for me.
7/26/2005 2:07 PM | eapen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET