B4J Question User defined short date and things I learned about dates - OH MY!

MrKim

Well-Known Member
Licensed User
Longtime User
Has anyone figured out how to get the user defined short date in windows? I can't believe it but it is apparently not built in to java. This makes it impossible to honor the users defined date format.

It is the key sShort at "HKEY_CURRENT_USER\Control Panel\International" in the registry.

In my research on this I did learn a few things. Here is the Java code to get the various date formats by LOCALE, which is not the same. As near as I can tell the Locale limits which date formats you can set for Long and short date but still gives you some freedom, for example M/d/yy and MM/dd/yyyy are both available for the short date for me, So is yyyy-mm-dd.

The output from the GetDate function is useful, as it seems to return the date in the universal ISO date format. yyy-MM-DD

This code is a modificatin of Erels InlineJava Example which is why it begins and ends with hello world..

Waiting for debugger to connect...
Program started.
Test = Hello world!
The short date format is M/d/yy
The MEDIUM date format is MMM d, yyyy
The LONG date format is MMMM d, yyyy
The FULL date format is EEEE, MMMM d, yyyy
GetDF = EEEE, MMMM d, yyyy
GetDate = 2020-04-20
GetDFByLocale = dd/MM/yy
Hello world from class!



B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    NativeMe = Me
    Dim s As String = NativeMe.RunMethod("Test", Null)
    Log("Test = " & s)
    s = NativeMe.RunMethod("GetDF", Null)
    Log("GetDF = " & s)
    s = NativeMe.RunMethod("GetDate", Null)
    Log("GetDate = " & s)
    s = NativeMe.RunMethod("GetDFByLocale", Null)
    Log("GetDFByLocale = " & s)
    Dim c As Class1
    c.Initialize
'    Dim Reg As
End Sub

#If JAVA

public static String Test() { //<-- static method
    return "Hello world!";
}

import java.util.*;
import java.text.*;

import java.time.*;


public static String GetDF() { //<-- static method
   SimpleDateFormat df = (SimpleDateFormat)
      DateFormat.getDateInstance(DateFormat.SHORT);  //SHORT MEDIUM LONG FULL
    return df.toPattern();
  }

  public static LocalDate GetDate() {
    LocalDate myObj = LocalDate.now(); // Create a date object
     return myObj;
  }
 

public static String GetDFByLocale() { //<-- static method
    Locale loc = Locale.ITALY;
    SimpleDateFormat df = (SimpleDateFormat)
    DateFormat.getDateInstance(DateFormat.SHORT, loc);  //SHORT MEDIUM LONG FULL
    return df.toPattern();
}
#End If

I found this which is purported to get the user defined date format but I can't get it to work.
B4X:
private static DateFormat getSystemDateFormat() throws ReflectiveOperationException {
       Class<?> clazz = Class.forName("sun.util.locale.provider.HostLocaleProviderAdapterImpl");
        Method method = clazz.getMethod("getDateFormatProvider");
        DateFormatProvider dateFormatProvider = (DateFormatProvider)method.invoke(null);
        DateFormat dateFormat = dateFormatProvider.getDateInstance(DateFormat.MEDIUM, Locale.getDefault(Locale.Category.FORMAT));
        return dateFormat;
    }

It is looking for HostLocaleProviderAdapterImpl and I don't know where to get it.

Also found this which is supposed to set your entire java instance to use user settings but it is looking for HOST and I don't know what that is.

B4X:
public static void SetLocal() {
//java.locale.providers=HOST;
}
 

MrKim

Well-Known Member
Licensed User
Longtime User
It is indeed not supported by Java and even if you find a hack that works it will not affect DateTime format.
I can set the format to be what the user has set correct? I.E. M/d/yy vs mm/dd/yyyy.
 
Upvote 0
Top