B4J Question [Solved] Is datatime conversion reliable in webapp concurrent threads

billzhan

Active Member
Licensed User
Longtime User
Hi,

In my webapp, datetime conversion are used quite a lot in concurrent threads.

B4X:
'For all threads,datetime conversion config:
DateTime.SetTimeZone(0)
DateTime.DateFormat="yyyy-MM-dd"
DateTime.TimeFormat="HH:mm:ss"
'conversion or get hour/minute
Dim tick As Long=DateTime.DateParse(datestr)
Dim minute As Int=DateTime.GetMinute(DateTime.Now)
Dim hour As Int=DateTime.GetHour(ticks)
   

'But I also need to use DateUtils.SetDateAndTime2 with different time-zones.
Public Sub SetDateAndTime2(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int, timezone As Double) As Long
    Dim df As String = DateTime.DateFormat
    DateTime.DateFormat = "GGyyyyMMddHHmmssz"
    ...
    Try
        Dim ticks As Long = DateTime.DateParse(d)
    Catch
    ....
    End Try
    DateTime.DateFormat = df
    Return ticks
End Sub

I know that conversions are done within very short time(1ms).
But it's possible that two concurrent threads use different DateFormat/SetTimeZone for conversion at the "same" time. Is this reliable?

One more question:
Do I have to set DateTime.SetTimeZone/DateFormat/TimeFormat every time before conversion, or just once in the main thread?


Best regards,
bz
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Good questions. The answer is yes. B4J DateTime object is thread safe (unlike B4A DateTime).
Do I have to set DateTime.SetTimeZone/DateFormat/TimeFormat every time before conversion, or just once in the main thread?
Each thread works with his own DateTime instance so you need to call DateTime.SetTimeZone in the servlet module.
 
Upvote 0
Top