Android Question Parse date crash

App Dude

Active Member
Licensed User
Longtime User
This mostly works, but sometimes it crashes. Anyone know why?
B4X:
Main.depTime = DateTime.DateParse(cbSelDate.SelectedItem) + (cbSelHour.SelectedItem * DateTime.TicksPerHour) + (cbSelMin.SelectedItem * DateTime.TicksPerMinute)

1635790396306.png



More context: I created a date-time picker which, for user simplicity, has only the next 5 days in a comboBox to select from., and hours and minutes in other comboBoxes. The dates in the date comboBox are in the format "Mon 11/01/2021".

For now, I've worked around it by saving the date of the first item in the date comboBox as ticks in a variable, and using that plus selected index to calculate the selected date in ticks.
 
D

Deleted member 103

Guest
What does this result look like?
B4X:
log(cbSelDate.SelectedItem)
log(cbSelHour.SelectedItem)
log(cbSelMin.SelectedItem)
 
Upvote 0

App Dude

Active Member
Licensed User
Longtime User
1635791966480.png


The crash report indicates that the date is the issue, not the hours or minutes, but the same date will work most of the time.
 
Upvote 0
D

Deleted member 103

Guest
and what does that output ?
B4X:
DateTime.DateParse(cbSelDate.SelectedItem)
 
Upvote 0
D

Deleted member 103

Guest
this one works
B4X:
    DateTime.DateFormat="EEE MM/dd/yyyy"
    Log(DateTime.DateParse("Montag 11/01/2021"))

but this one does not
B4X:
    DateTime.DateFormat="EEE MM/dd/yyyy"
    Log(DateTime.DateParse("Mon 11/01/2021"))
 
Upvote 0

App Dude

Active Member
Licensed User
Longtime User
So it works 9 out of 10 times, and when it crashes, I can re-do it with the same date and it works.
You can see in the crash that it has a properly formatted date. I suspect the answer will come from the Java dump, but I don't know what that's saying.
 
Upvote 0

App Dude

Active Member
Licensed User
Longtime User
This is how I create the list...

B4X:
        ' Create list of allowed dates...
        Dim selDateList As List
        selDateList.Initialize
        DateTime.DateFormat = "E MM/dd/yyyy"
        Dim i As Int
        For i = 0 To 4
            selDateList.Add(DateTime.Date(DateTime.Now + (DateTime.TicksPerDay * i)))
        Next
        cbSelDate.setitems(selDateList)
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
not problem
ref:
B4X:
'    Create list of allowed dates...
    Dim selDateList As List
    selDateList.Initialize
    
    DateTime.DateFormat = "E MM/dd/yyyy"
    
    Log($"DateFormat: ${DateTime.DateFormat}"$)
    Log($"Device DateFormat: ${DateTime.DeviceDefaultDateFormat}"$)
    Log($"Device TimeFormat: ${DateTime.DeviceDefaultTimeFormat}"$)

    For i = 0 To 29
        selDateList.Add(DateTime.Date(DateTime.Now + (DateTime.TicksPerDay * i)))
    Next

    For Each s As String In selDateList
        Dim ticks As Long = DateTime.DateParse(s)
        Dim Date As String = DateTime.Date(DateTime.DateParse(s))
        Log($"List: ${s} / Ticks: ${ticks} / Date: ${Date}"$)
    Next

1635811346003.png
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
I think the problem must be when you assign the dates in the list and in the combo box as a string with the format "E MM/dd/yyyy".

That means you have to rescue the data from the combo box and the list with the format used.

If I use the "E MM/dd/yyyy" format, you must assign this format before converting it to ticks.

sample:
B4X:
DateTime.DateFormat="E MM/dd/yyyy"
Main.depTime = DateTime.DateParse(cbSelDate.SelectedItem) + (cbSelHour.SelectedItem * DateTime.TicksPerHour) + (cbSelMin.SelectedItem * DateTime.TicksPerMinute)

Note:
You must consider the language if you assign dates in string format.

English: Mon 11/01/2021
Spanish: Lun 11/01/2021
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
So it works 9 out of 10 times, and when it crashes, I can re-do it with the same date and it works.
That's weird. I'd be banging away at this angle. Perhaps it happens after you've used n different dates, and when you restart the program and re-enter the date that crashed it just previously, the "crash after n different dates" feature counter ? has reset to zero.
 
Upvote 0
Top