Android Question Error while converting Json date to long value

Makumbi

Well-Known Member
Licensed User
B4X:
no extras
Token( at character 0 of [{"Account":"19-07780","sms":"I wanted to take namuleme for treatment please help","Datesent":"\/Date(1586775195000)\/","Phone":"0782911364","Status":"Private Sms","Type":"Father","Code":null,"atk":37534}])
Account: 19-07780
sms: I wanted to take namuleme for treatment please help
Datesent: /Date(1586775195000)/
Status: Private Sms
codes: null
atk: 37534
13/04/2020 01:53:15 pm
Error occurred on line: 161 (SendsmschatV2)
java.lang.NumberFormatException: For input string: "13/04/2020 01:53:15 pm"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at com.kccug.kabojjajuniorapp.sendsmschatv2$ResumableSub_replysms.resume(sendsmschatv2.java:699)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)

B4X:
[{"Account":"19-07780","sms":"I wanted to take namuleme for treatment please help","Datesent":"\/Date(1586775195000)\/","Phone":"0782911364","Status":"Private Sms","Type":"Father","Code":null,"atk":37534}]


error comes here

B4X:
    Dim holds As Long = getdate(JsonDateToTick( quot.Get("Datesent")))

my date conversion code

B4X:
Sub JsonDateToTick(s As String) As Long
    Dim m As Matcher = Regex.Matcher("\d+", s)
    If m.Find Then
        Return m.Match
    End If
    Log("Invalid date: " & s)
    Return 0
End Sub

Sub  getdate(gt As String) As String
    Dim lt As Long
    DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a" ' "1961-08-29"
    Dim datestring As String = DateTime.Date(( gt))
    lt = DateTime.DateParse(datestring)
    DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a"
    Log(DateTime.Date(lt))
    'gt=DateTime.Date(lt)
    Return DateTime.Date(lt)
    
End Sub
 

Mahares

Expert
Licensed User
Longtime User
error comes here
I think your code should be:
B4X:
Log(JsonDateToTick("/Date(1586775195000)/"))  '<------  "/Date(1586775195000)/" is quot.Get("Datesent")
    Log(GetDate(JsonDateToTick("/Date(1586775195000)/")))  'displays: 13/04/2020 06:53:15 AM
B4X:
Sub JsonDateToTick(s As String) As Long
    Dim m As Matcher = Regex.Matcher("\d+", s)
    If m.Find Then
        Return m.Match
    End If
    Log("Invalid date: " & s)
    Return 0
End Sub

Sub GetDate(lt As Long) As String
    DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a"   
    Return DateTime.Date(lt)
End Sub
 
Upvote 0
Top