B4J Question How to parse Belgian (Dutch) DateTimes?

MathiasM

Active Member
Licensed User
Hello

I'm trying to parse this datetime: 18-mrt-2021 10:43:18
mrt is short for Maart, which is March in Dutch.

I tried this:
B4X:
DateTime.TimeFormat = "dd-MMM-YYYY HH:mm:ss"

But the datetime was unparsable. I thought it might have somehting to do with localization settings. I found this website: https://www.localeplanet.com/java/nl-NL/index.html
Great, the short months on that page show: jan., feb., mrt., apr., mei, jun., jul., aug., sep., okt., nov., dec.

So that could (should) work? I tried setting the locale with this code:
B4X:
    Dim jo As JavaObject
    jo.InitializeStatic("java.util.Locale").RunMethod("setDefault", Array(jo.GetField("nl_NL")))

However, an error is thrown stating
java.lang.RuntimeException: Field: nl_NL not found in: java.util.Locale

What is wrong here? To be fair: I'm only guessing that localization has something to do with my problem. If you spot another problem, great!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
    For Each locale As JavaObject In FindAllLocales
        Dim s As String = locale 'ignore
        If s.ToLowerCase.Contains("nl") Then
            Log(s)
        End If
    Next
    SetDefaultLocale("nl", "NL")
    DateTime.DateFormat = "dd-MMM-YYYY HH:mm:ss"
    Log(DateTime.Date(DateTime.Now))
End Sub

Private Sub SetDefaultLocale(Language As String, Country As String)
    Dim loc As JavaObject
    loc.InitializeNewInstance("java.util.Locale", Array(Language, Country))
    Dim jo As JavaObject
    jo.InitializeStatic("java.util.Locale").RunMethod("setDefault", Array(loc))
End Sub

Sub FindAllLocales As List
    Dim jo As JavaObject
    Dim res() As Object = jo.InitializeStatic("java.util.Locale").RunMethod("getAvailableLocales", Null)
    Return res
End Sub

You don't need to use FindAllLocales.
You can assume that nl-NL will be there (https://www.oracle.com/java/technologies/javase/jdk8-jre8-suported-locales.html).
 
Upvote 0
Top