Se Song Se Song - 4 months ago 29x Question

Convert long value from java Date.getTime() to DateTime in VB.Net

I have two difference application, one using java and other using VB.NET. The java application converted object to json and save to database as text as bellow


perform_date:1462035600000 in java equal to "Sun May 01 00:00:00 ICT 2016", but I try to convert this number to DateTime in VB.NET but it show different date "01-Jan-01 12:00:00 AM".

Test VB.NET Code

Dim d = New Date()
Console.WriteLine(CType(d, Date).ToString())


01-Jan-01 12:00:00 AM

Test Java Code:

long d = Long.parseLong("1462035600000");


Sun May 01 00:00:00 ICT 2016

What I did is wrong, I'm a newby on VB.NET. Please help


Java use unix timestamp. You need to initiate your Date at January 1st, 1970

If you are in the ICT timezone, you could probably use a datetime using LocaL as datetime kind. Otherwise, you can get the datetime as an UTC datetime, then manually add 7 hours to it.

Dim JavaTimeStamp As Long = 1462035600000
Dim MyDateTimeUTC as DateTime = GetUTCDateFromTimeStamp(JavaTimeStamp).addhours(7) 'Add 7 hours to match current ICT time (I had no consideration for saving time)
Dim MyDateTimeLocal as DateTime = GetLocalDateFromTimeStamp(JavaTimeStamp)


Public Function GetUTCDateFromTimeStamp(TimeStamp as long) As DateTime
    static startTime As New DateTime(1970, 1, 1, 0, 0, 0,DateTimeKind.Utc)
    return Starttime.AddMilliseconds(TimeStamp)
End Function

Public function GetLocalDateFromTimeStamp(TimeStamp as long) as DateTime
      static startTime As New DateTime(1970, 1, 1, 0, 0, 0,DateTimeKind.Local)
    return Starttime.AddMilliseconds(TimeStamp)
End function