Android Question how display todays date and time

Makumbi

Well-Known Member
Licensed User
Please help how can i today's date and then convert it to show current date and time i have tried but i get this eer
B4X:
Sub Timestamp_Brasileiro(dataTimeStamp As String) As String
    DateTime.DateFormat = "dd/MM/yyyy" : DateTime.TimeFormat = "HH:mm:ss"
    Dim dataTempo() As String  = Regex.Split(" ", dataTimeStamp)
    Private ltempo As Long = DateTime.DateTimeParse(dataTempo(0),dataTempo(1))
    DateTime.DateFormat = "dd/MM/yyyy" : DateTime.TimeFormat = "HH:mm"
    Return DateTime.Date(ltempo) & " " & DateTime.Time(ltempo)
End Sub

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 60 (Main)
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
    at java.lang.reflect.Array.get(Array.java:170)
    at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
    at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:588)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:387)
    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 de.amberhome.slidemenuexample.main.afterFirstLayout(main.java:104)
    at de.amberhome.slidemenuexample.main.access$000(main.java:17)
    at de.amberhome.slidemenuexample.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 

mangojack

Expert
Licensed User
Longtime User
What is the value of passed variable ... dataTimeStamp ?

B4X:
Dim dataTempo() As String  = Regex.Split(" ", dataTimeStamp)
my guess is dataTempo only recieved on value ... so trying to parse the value of dataTempo(1) causes error.

Why are you setting DateFormat twice ?

it appears you just passing a Date Time string and returning another with a different time format ?



*ps Add some logs to you above code and test outputs to see what is going wrong !!!
 
Last edited:
Upvote 0

Brandsum

Well-Known Member
Licensed User
Your code is working fine here. As you are spliting the given value with space delimiter, it is better to check if the given value has a space or not before using.
B4X:
Sub Timestamp_Brasileiro(dataTimeStamp As String) As String
    If Not(dataTimeStamp.Contains(" ")) Then Return "Invalid Format" 'just check if the given value has space or not
 
    DateTime.DateFormat = "dd/MM/yyyy" : DateTime.TimeFormat = "HH:mm:ss"
    Dim dataTempo() As String  = Regex.Split(" ", dataTimeStamp)
    Private ltempo As Long = DateTime.DateTimeParse(dataTempo(0),dataTempo(1))
    DateTime.DateFormat = "dd/MM/yyyy" : DateTime.TimeFormat = "HH:mm"
    Return DateTime.Date(ltempo) & " " & DateTime.Time(ltempo)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Log(Timestamp_Brasileiro("10/02/2019 20:33:12"))
End Sub

But as you just want to remove second from the datetime string, you can do this using substring,
B4X:
Sub Timestamp_Brasileiro(dataTimeStamp As String) As String
    Return dataTimeStamp.SubString2(0,dataTimeStamp.Length - 3)
End Sub
 
Last edited:
Upvote 0

asales

Expert
Licensed User
Longtime User
I think your error message is not related to the date/time, but to this component:
amberhome.slidemenuexample
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…