0

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?

BanzaiTokyo
  • 1,376
  • 4
  • 17
  • 33
  • Possible duplicate of [SWT DateTime format change](http://stackoverflow.com/questions/34632492/swt-datetime-format-change) – Rüdiger Herrmann Sep 29 '16 at 15:54
  • @RüdigerHerrmann I just want to point out that this is not a duplicate question and I edited the post to explain why. – BanzaiTokyo Sep 30 '16 at 07:33
  • 1
    I still think this is a duplicate of http://stackoverflow.com/questions/34632492/swt-datetime-format-change. Read the answers and you will learn that the SWT DateTime does not care about Java locale changes because it uses the OS date format. And that answers your question as well, right? – Rüdiger Herrmann Sep 30 '16 at 07:52
  • @RüdigerHerrmann could you please explain what OS date format is? when I type `date` in console, I get `ven. sept. 30 11:22:03 CEST 2016` which is not the format that DateTime displays. – BanzaiTokyo Sep 30 '16 at 09:25
  • @RüdigerHerrmann I will also mention that `locale` returns: `LANG=fr_FR.UTF-8 LC_CTYPE="fr_FR.UTF-8" LC_NUMERIC="fr_FR.UTF-8" LC_TIME="fr_FR.UTF-8" LC_COLLATE="fr_FR.UTF-8" LC_MONETARY="fr_FR.UTF-8" LC_MESSAGES="fr_FR.UTF-8" LC_PAPER="fr_FR.UTF-8" LC_NAME="fr_FR.UTF-8" LC_ADDRESS="fr_FR.UTF-8" LC_TELEPHONE="fr_FR.UTF-8" LC_MEASUREMENT="fr_FR.UTF-8" LC_IDENTIFICATION="fr_FR.UTF-8" LC_ALL=` – BanzaiTokyo Sep 30 '16 at 09:27
  • SWT `DateTime` will use the date format that your OS uses, e.g. if your OS is set to US English, it'll use MM/dd/yyyy while if your OS is set to UK English it'll use dd/MM/yyyy. It doesn't matter what locale Java uses internally, `DateTime` doesn't care. – Baz Sep 30 '16 at 09:54
  • Some information about the Windows time format here: http://www.sysprobs.com/change-date-format-windows-7-ddmmyyyy – Baz Sep 30 '16 at 09:55
  • @Baz As you see in my comments, my os is set to fr_FR and DateTime still returns MM/dd/yyyy - and this is exactly my problem. BTW, I am on Linux. – BanzaiTokyo Sep 30 '16 at 11:17
  • @RüdigerHerrmann please take a look at Baz's answer - it confirms that this is not a duplicate and it was not answered in the other thread. – BanzaiTokyo Sep 30 '16 at 13:15

1 Answers1

1

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();
}
Baz
  • 36,440
  • 11
  • 68
  • 94
  • Perfect! this works like a charm! Especially cool that you can change the locale from code and be independent from OS settings. Cheers! – BanzaiTokyo Sep 30 '16 at 15:48