Android Question Time Format Problem

khwarizmi

Active Member
Licensed User
Longtime User
Hi all
I use this code to get number of milliseconds from the beginning of the day:

B4X:
DateTime.TimeFormat = "HH:mm:ss.SSS"
Dim m As String=DateTime.Time(DateTime.Now)
Log(m)
' if the device default language is Arabic it gives ١٥:٢٩:١٧.٨٨٦ 
m=m.Replace(":"," ")
m=m.Replace("."," ")
Dim r() As String=Regex.Split(" ",m)
Dim q,qq,qqq,qqqq As Int
q=r(0) : qq=r(1) : qqq=r(2) : qqqq=r(4)
mls=q*3600000+qq*60000 +qqq*1000+qqqq

the code is working very well, But if the device default language is Arabic it gives this error:

B4X:
(NumberFormatException) java.lang.NumberFormatException: For input string: "١٥"
 

khwarizmi

Active Member
Licensed User
Longtime User
Solved!

I added this at the beginning of the cade:
B4X:
Dim ju As JavaObject
   ju.InitializeStatic("java.util.Locale").RunMethod("setDefault", Array(ju.GetField("US")))

from this post:
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
q=r(0) : qq=r(1) : qqq=r(2) : qqqq=r(4)
It should be : q=r(0) : qq=r(1) : qqq=r(2) : qqqq=r(3). It is probably a typo on your end.
Anyway, I think there is an easier way to get the results than with your code because DateTime.Time(DateTime.Now) is affected by the time zone.
Here is how I would have done it:
B4X:
DateTime.DateFormat="MM/dd/yyyy" 'or use your preferred format with the day first
    Dim ts As Long = DateTime.DateParse(DateTime.Date(DateTime.Now))
    DateTime.DateFormat="MM/dd/yyyy HH:mm:ss.SSS"
    Dim te As Long = DateTime.Now
    Dim t As Long =te-ts
    Log(t)
 
Upvote 0
Top