Android Question Parsing a date oddity

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I have been having problems parsing datetimes taken from a MYSQL database.

I have finally tracked down the problem to the Datetime.dateparse function.

Can anyone spot what is wrong here.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Private datestring = "2023-02-10T18:15:22.000000Z"
    
    
    Try   ' I would expect this to succeed
        DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.000000Z"
        Private da1 As Long = DateTime.DateParse(datestring)
        Log("Parse 1 success")
    Catch
        Log(LastException)
        Log("Date parse 1 failure")
    End Try
    
    Try  ' I would expect this to fail
        DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        Private da1 As Long = DateTime.DateParse(datestring)
        Log("Parse 2 success")
    Catch
        Log(LastException)
        Log("Date parse 2 failure")
    End Try
    
    
End Sub

output from log,

Logger connected to: Google Pixel 2 XL
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
(ParseException) java.text.ParseException: Unparseable date: "2023-02-10T18:15:22.000000Z"
Date parse 1 failure
Parse 2 success
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **

Why does the wrong date format parse the date correctly?

The same code runs as expected on iOS.
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I don't need the millliseconds. That is the string I get back from a DATETIME column of a MYSQL database.

I only need the DATE part so I ended up trauncating at the T.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
    Dim datestring As String = "2023-02-10T18:15:22.000000Z"
    Log(datestring)
    DateTime.SetTimeZone(0)
 
    Log("Parse 1 success")
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
    Dim da1 As Long = DateTime.DateParse(datestring)
    Log(DateTime.Date(da1))
    Log(DateTime.GetTimeZoneOffsetAt(da1))
 
    Log("Parse 2 success")
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    Dim da1 As Long = DateTime.DateParse(datestring)
    Log(DateTime.Date(da1))
    Log(DateTime.GetTimeZoneOffsetAt(da1))
1675961694879.png

Read: timezone
mysql
 
Last edited:
Upvote 1
Top