BanzaiTokyo BanzaiTokyo - 2 months ago 20
Linux Question

SWT DateTime doesn't take into account the locale

Is it possible to change to change the format in which DataTime widget diplays date? The matter is that even though I set the locale to one that uses European format (dd/mm/yyyy) I still have DateTime widget in mm/dd/yyyy format.

Edit: There have been similat questions on SO along the lines "How to change the format of DateTime" and they were answered by something like "change the locale and DateTime will adapt". My question is about the situation when changing the locale doesn't have any effect on DateTime widget. What might be wrong? Is there any other solution to force format change?

Baz Baz
Answer

After running some tests on Linux, I can confirm that the DateTime widget does not appear to be using the OS's locale. This seems to be a bug and you should report it.

What you can do in the meantime is use Nebula's CDateTime which supports Java Locales:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());

    Locale.setDefault(Locale.GERMAN);
    CDateTime cdt = new CDateTime(shell, CDT.DATE_SHORT);
    cdt.setSelection(new Date());

    Locale.setDefault(Locale.ENGLISH);
    cdt = new CDateTime(shell, CDT.DATE_SHORT);
    cdt.setSelection(new Date());

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Comments