Tag: convert

UTC date/time string to Java date in local time zone


Reference: https://www.w3.org/TR/NOTE-datetime

Uses joda-time (because yes, I still work in Java 1.6).  There is probably a simpler way.  But this works. This assumes all parts of the string are supplied.

public static Date convertUTCStringToLocalDateTime(String utcDate)
    DateTime utcDateTime = new DateTime(utcDate);
    Date localDateTime = utcDateTime.toDate();
    return localDateTime;

SQL Server datetime to int


declare @currentDateTime datetime 
declare @timeStampValue int 

set @currentDateTime = SYSDATETIME(); 
print @currentDateTime 
set @timeStampValue = DATEDIFF(second, '1970-01-01', @currentDateTime) 
print @timeStampValue

To check the result:



UTC milleseconds example:

declare @timeStampValue Bigint  
declare @utcDateTime datetime 

set @utcDateTime = GETUTCDATE(); 
set @timeStampValue = DATEDIFF(SECOND, '1970-01-01', @utcDateTime) 
set @timeStampValue = @timeStampValue * 1000