B4J Code Snippet DateTime minus milliseconds

Although I found a few examples I couldnt find an example of deducting milliseconds from a datetime.

Anyway I am processing some server log files that store a log entry with a datetime with milliseconds as a string eg "2016-07-26T15:31:57,619".

The log also records the elapsed time in milliseconds of the transaction (ie how long the transaction took to process).

I needed to work out when the transaction started (ie log time minus the elapsed time).

Below is the sub I wrote, it returns a string in the same format.

B4X:
Sub CalcStartTime(time As String, elapsed As Double) As String
    DateTime.DateFormat = "yyyy-MM-dd"
    DateTime.TimeFormat = "HH:mm:ss.SSS"       
       
    Dim time_comp As String = time.SubString(time.LastIndexOf("T")+1)
    Dim date_comp As String = time.SubString2(0,time.LastIndexOf("T"))
   
    Dim t1 As Long
   
    t1 = DateTime.DateTimeParse(date_comp,time_comp)
    t1 = t1 - elapsed*1000
   
    Return (DateTime.Date(t1) & "T" & DateTime.Time(t1))
End Sub
 
Top