Android Question Date Time Help(SOLVED)

Stichler

Active Member
Licensed User
Hi all I have an app where user enters a date and time and then clicks a button to get time remaining. I borrowed some code found on Forums but having problem getting it to work. Maybe someone can help or give better way to accomplish this?

B4X:
Sub btnCount_Click
    Dim dateString As String = Date1 & "," & Time1
    Dim date, time As Long
    Dim parts() As String = Regex.Split(", ", dateString)
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm"
    date = DateTime.DateParse(parts(0))
    time = DateTime.TimeParse(parts(1)) - DateTime.DateParse(DateTime.Date(DateTime.Now)) 
    Log(ConvertTicksToTimeString(date + time - DateTime.Now))
   End Sub
   
   
Sub ConvertTicksToTimeString(t As Long) As String
    Dim days, hours, minutes, seconds As Int
    days = t / DateTime.TicksPerDay
    hours = (t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return days & " days, " & NumberFormat(hours, 2, 0) & " hours, " _
        & NumberFormat(minutes, 2, 0) & " minutes and " & NumberFormat(seconds, 2, 0) & " seconds"
End Sub

This is the error I get:

** Activity (main) Resume **
Error occurred on line: 68 (Main)
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at java.lang.reflect.Array.get(Array.java:174)
at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:593)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:392)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6891)
at android.widget.TextView.performClick(TextView.java:12651)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 

JordiCP

Expert
Licensed User
Longtime User
This error line
B4X:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
says that, somewhere, you are trying to access index 1 of an array that only has one element (as arrays start with index 0, only that one will be available)

Looking at your code, seems that the Regex.Split method only returned one element in the array, when you were expecting 2. I guess that's because there is an extra space after the comma in the pattern (should be "," instead of ", "), so it returns just the same original string and length is 1
B4X:
Sub btnCount_Click
    Dim dateString As String = Date1 & "," & Time1
    Dim date, time As Long
    Dim parts() As String = Regex.Split(", ", dateString)  '<--- Probably the error is in ", " pattern. It should be "," (without the trailing space)
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm"
    date = DateTime.DateParse(parts(0))
    time = DateTime.TimeParse(parts(1)) - DateTime.DateParse(DateTime.Date(DateTime.Now)) '<-- you are assuming, without checking, that parts() has at least 2 elements 
    Log(ConvertTicksToTimeString(date + time - DateTime.Now))
   End Sub
 
Upvote 0
Top