Android Question Get date/time from jRDC2 server and convert to local time

Binary01

Active Member
Licensed User
Longtime User
Hi,
My jRDC2 server is running!.

Now, I want to get date/time from server and convert it to local time in my b4a project.

Firstly I get date/time from server

B4X:
Sub ConnectionTest
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("http://201.67.166.90:17180/test")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        lblStatus.Text = j.GetString
        Dim str1 As String = j.GetString

        'xxxxxxxxxxxxxxxx
        'I want to convert str1 to local time zone and show.
        'xxxxxxxxxxxxxxxx

        If str1.Contains(" successful") Then
            lblLocalTime.Text = str1.SubString2(24,44)
        Else
            lblLocalTime.Text =""
        End If
    Else
        Log("Connect error")
        lblStatus.Text ="Error connection"
    End If
    j.Release
 
End Sub

Next, I want to convert str1 (that contain server date/time connection successful)
to local time and show.

Thanks
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.
 
Upvote 0

Binary01

Active Member
Licensed User
Longtime User
Will you post the solution?
Sure,

B4X:
Sub ConnectionTest
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(rdCheck)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
 
        Dim str1 As String = j.GetString
        If str1.Contains("successful") Then
            str1 = str1.SubString2(25,44)  'get date time from string
          
            Do While str1.Contains(" ")
                str1 = str1.Replace(" ","|")
            Loop
          
            Dim serverDateTime() As String
            serverDateTime = Regex.Split("\|", str1.Trim)
          
            Log(serverDateTime(0))
            Log(serverDateTime(1))
          
            DateTime.DateFormat = "MM/dd/yyyy"
            DateTime.TimeFormat = "HH:mm:ss"
          
            Dim t As Long = DateTime.DateTimeParse(serverDateTime(0), serverDateTime(1))

            Dim addTimeZoneOffsetTicks As Long
            Dim fTimeZoneOffset As Float
            fTimeZoneOffset = DateTime.GetTimeZoneOffsetAt(t)
          
            addTimeZoneOffsetTicks = fTimeZoneOffset * (60*60*1000)
          
            Log(DateTime.Date(t) & " " & DateTime.Time(t))
           
            'lblLocalTime.Text = DateTime.Time(t)
          
            Log(DateTime.TimeZoneOffset)
            Log(DateTime.Now)
          
            Dim strDate As String
            Dim strTime As String
          
            DateTime.DateFormat="MM/dd/yyy HH:mm:ss"
            Dim strDate As String=DateTime.Date(DateTime.Now)
            Dim strTime As String= DateTime.Time(DateTime.Now)
          
            Dim n As Long = DateTime.DateTimeParse(strDate, strTime)
            Log(n)
            Log(t)
            Log(addTimeZoneOffsetTicks)
          
            Dim a As Long
            a = t + addTimeZoneOffsetTicks
          
            Log(a)
          
            Log(strDate)
            Log(strTime)
          
            Log(DateUtils.TicksToString(a))
            lblLocalTime.Text =DateUtils.TicksToString(a)
          
        Else
            'lblLocalTime.Text =""
        End If
      
    Else
        Log("Connect error")
        'lblStatus.Text ="Error connection"
    End If
    j.Release
  
End Sub
 
Upvote 0
Top