Android Question Problem comparing DateTime.Date

José J. Aguilar

Expert
Licensed User
Hi all:

I'm selecting a date in a B4XPreferenceDialog, and after that, I want to compare that date with some dates in a xCLV to insertAt ordered by date.
See the comments in the code to see the problem, probably I'm missing something
B4X:
   If Result = xui.DialogResponse_Positive Then
        Log(options1)  'Log output: (MyMap) {Date=1621288800000}
        Log(DateTime.Date(options1.Get("Date"))) 'Log output: 05/18/2021
        Log(DateTime.Date(DateTime.Now)) 'Log output: 05/18/2021
        Log(DateTime.Date(DateTime.Now) & " : " & DateTime.Date(options1.Get("Date"))) 'Works Log output: 05/18/2021 : 05/18/2021. IT FAILS IF YOU CHANGE ORDER (DateTime.Now in second place) or if you try to compare with =, <, >
        Log(DateTime.Date(options1.Get("Date")& " : " & DateTime.Date(DateTime.Now)  )) 'FAIL java.lang.NumberFormatException: For input string: "1621288800000 : 05/18/2021"
        Log(DateTime.Date(DateTime.Now) > DateTime.Date(options1.Get("Date"))) 'FAIL
    End If


B4X:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
(MyMap) {Date=1621288800000}
05/18/2021
05/18/2021
Ha ocurrido un error en la línea: 39 (B4XMainPage)
java.lang.NumberFormatException: For input string: "05/18/2021"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:543)
    at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:134)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:136)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:85)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1086)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

See attached a B4XPages project showing the problem
 

Attachments

  • TestDate.zip
    14.6 KB · Views: 104

Erel

B4X founder
Staff member
Licensed User
Longtime User
Expected to fail:
B4X:
Log(DateTime.Date(options1.Get("Date")& " : " & DateTime.Date(DateTime.Now)  ))
It should be:
B4X:
Log(DateTime.Date(options1.Get("Date")) & " : " & DateTime.Date(DateTime.Now))

Expected to fail:
B4X:
Log(DateTime.Date(DateTime.Now) > DateTime.Date(options1.Get("Date"))) 'FAIL
It should be:
B4X:
Log(DateTime.Now > options1.Get("Date"))
DateTime.Date returns a string. You cannot compare it as number.

I recommend you to start with::
B4X:
Dim ticks As Long = options1.Get("Date")
This is not related to the previous errors. This way you will be sure that the value is treated as Long and not implicitly cast to other types.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It should be:
It may be a bit more nuanced. If comparing a complete timestamp, then yes, but here it looks like the OP may want to compare only the date portion. To show the difference in results (and one way to compare the date portion):
B4X:
    'Let's capture some timestamps and compare them
    Dim aTime As Long = DateTime.Now
    Sleep(100)
    Dim bTime As Long = DateTime.Now
    Log($"aTime: ${aTime}, bTime: ${bTime}"$)
    Log(aTime = bTime) ' false
    Log(bTime > aTime) ' true
    'Let's extract the date portion of the timestamps and compare them
    Dim aaTime As Long = DateTime.DateParse(DateTime.Date(aTime))
    Dim bbTime As Long = DateTime.DateParse(DateTime.Date(bTime))
    Log($"aaTime: ${aaTime}, bbTime: ${bbTime}"$)
    Log(aaTime = bbTime) ' true
    Log(bbTime > aaTime) ' false
Note: Please note this makes the assumption that you run the code during a timeframe where aTime and bTime occur on the same date. There could be a ever so slight chance the date changes in the 100ms the code waits to capture the the second timestamp and then the test results would differ.
 
Last edited:
Upvote 0
Top