B4J Question Formatted date in one shot

marcick

Well-Known Member
Licensed User
Longtime User
In various parts of my B4J server app, i need to obtain a datetime with different dateformat.
Actually, I backup and restore the current dateformat when I need, like this:

B4X:
Dim df As String=DateTime.DateFormat
DateTime.DateFormat="E dd/MM HH:mm:ss"
ntext=DateTime.date(DateTime.Now)
DateTime.DateFormat=df

But with many parallel processes sometimes I have troubles.
Is there a method to obtain a datetime with a particular format in one single instruction ?
 

Cableguy

Expert
Licensed User
Longtime User
Is there a method to obtain a datetime with a particular format in one single instruction ?

Yes... create a sub that takes as an argument the date value you want to parse... then you just have to call that sub!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
B4X:
SUB MyDate
Dim df As String=DateTime.DateFormat
DateTime.DateFormat="E dd/MM HH:mm:ss"
Dim CDate as String = DateTime.date(DateTime.Now)
DateTime.DateFormat=df
Return cDate
End sub

Then you just...
Dim CurrentDate as String = MyDate
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
So easy ?
And the sub is fully executed without interruptions of other parallel processes ? Not possible that another process modify the dateformat before the sub is completed ?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
They are processed "in line" meaning that your code just jumps to the sub and then returns to the next line after the jump...

So each call will have it's return... This is not multi-thread safe afaik....
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
So I'm still in troubles.

If after
B4X:
"DateTime.DateFormat="E dd/MM HH:mm:ss""

and before
B4X:
Dim CDate as String = DateTime.date(DateTime.Now)

another process change the dateformat then my date is not formatted as I want exiting the sub.
Correct ?
No solutions ?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Set a flag, use the new "wait for"... There are many solutions to prevent your issue...
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Does it look ok ?

B4X:
Public Sub MyDate (date As Long, format As String) As ResumableSub
    Do Until Main.DateFormatFlag=False
        Sleep(0)
    Loop
    Main.DateFormatFlag=True
    Dim dform As String=DateTime.DateFormat
    DateTime.DateFormat =format
    Dim md As String=DateTime.Date(date)
    DateTime.DateFormat=dform
    Main.DateFormatFlag=False
    Return md
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I think it would be better that this part of the code be in your calling sub
B4X:
    Do Until Main.DateFormatFlag=False
        Sleep(0)
    Loop

Then it would be something like...

B4X:
    Do while Main.DateFormatFlag=False 'The sub is still occupied by another operation
        Sleep(0)
    Loop
Mydate(.... , .....)

And then...
B4X:
Public Sub MyDate (date As Long, format As String) As ResumableSub
Main.DateFormatFlag=False 'Just to let all other calling subs know this is occupied
    Dim dform As String=DateTime.DateFormat
    DateTime.DateFormat =format
    Dim md As String=DateTime.Date(date)
    DateTime.DateFormat=dform
    Main.DateFormatFlag=True 'This would mean that the sub is free for new operations
    Return md
End Sub

Edited code, a "Main.DateFormatFlag=False" was missing
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thanks.
A “Main.DateFormatFlag=False” is required somewhere also.
But isn’t my version more clean ?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
If you make the check for the flag inside the called sub, every time it gets called the flag will be reset, and may never return to the caller sub...

Anyway, for me all this is theoretical, as I have not tested any of this... especially multi-threaded
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
B4X:
Sub ConvertDate(DateTimeString As String, DateTimeFormat As String, NewDateTimeFormat As String,Language As String,Country As String) As String
    Dim JOLocale As JavaObject
    Dim JODTConvertTo As JavaObject
    Dim JODTConvertFrom As JavaObject
    Dim JODate As JavaObject
 
    JOLocale.InitializeNewInstance("java.util.Locale",Array As Object(Language,Country))
    JODTConvertTo.InitializeNewInstance("java.text.SimpleDateFormat",Array As Object(NewDateTimeFormat,JOLocale))
    JODTConvertFrom.InitializeNewInstance("java.text.SimpleDateFormat",Array As Object(DateTimeFormat))
    JODate  = JODTConvertFrom.RunMethod("parse",Array As Object(DateTimeString))
    Return JODTConvertTo.RunMethod("format",Array As Object(JODate))
End Sub

Calling the sub:

B4X:
Log(ConvertDate("2018-06-23 11:00:00", "yyyy-MM-dd HH:mm:ss", "EEE dd.MM.yyyy HH:mm","it",""))
Log(ConvertDate("2018-06-23 11:00:00", "yyyy-MM-dd HH:mm:ss", "EEE dd/MM/yyyy HH:mm","gb",""))

Gives:

sab 23.06.2018 11:00
Sat 23/06/2018 11:00
 
Last edited:
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
B4X:
Sub DateToFormat(Date  As Long, DateTimeFormat As String,Language As String,Country As String) As String    'ignore
    Dim JOLocale As JavaObject
    Dim JODTConvertTo As JavaObject
    Dim JODate As JavaObject
    JODate.InitializeNewInstance("java.util.Date",Array (Date))
    JOLocale.InitializeNewInstance("java.util.Locale",Array As Object(Language,Country))
    JODTConvertTo.InitializeNewInstance("java.text.SimpleDateFormat",Array As Object(DateTimeFormat,JOLocale))
    Return JODTConvertTo.RunMethod("format",Array As Object(JODate))
End Sub
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
B4X:
Sub ConvertDate(DateTimeString As String, DateTimeFormat As String, NewDateTimeFormat As String,Language As String,Country As String) As String
    Dim JOLocale As JavaObject
    Dim JODTConvertTo As JavaObject
    Dim JODTConvertFrom As JavaObject
    Dim JODate As JavaObject
 
    JOLocale.InitializeNewInstance("java.util.Locale",Array As Object(Language,Country))
    JODTConvertTo.InitializeNewInstance("java.text.SimpleDateFormat",Array As Object(NewDateTimeFormat,JOLocale))
    JODTConvertFrom.InitializeNewInstance("java.text.SimpleDateFormat",Array As Object(DateTimeFormat))
    JODate  = JODTConvertFrom.RunMethod("parse",Array As Object(DateTimeString))
    Return JODTConvertTo.RunMethod("format",Array As Object(JODate))
End Sub

Calling the sub:

B4X:
Log(ConvertDate("2018-06-23 11:00:00", "yyyy-MM-dd HH:mm:ss", "EEE dd.MM.yyyy HH:mm","it",""))
Log(ConvertDate("2018-06-23 11:00:00", "yyyy-MM-dd HH:mm:ss", "EEE dd/MM/yyyy HH:mm","gb",""))

Gives:

sab 23.06.2018 11:00
Sat 23/06/2018 11:00
A simpler way is to use the jCalendar library.
 
Upvote 0
Top