4 digits year in DateField

I would like to display 4 digits year in the date field. (As recommended, I want to achieve this from the Component’s Locale settings). How does it determine whether 2 digits or 4 digits are displayed? If it i using the DateFormat instance, is it a way to use some other style instead of the SHORT style - DateFormat.getInstance(DateFormat.SHORT, locale)?
I have different users and their Locale settings are different. I set it at the application level when they log in.

The DateField uses the SHORT style dateformat for the locale by default. If you want to set a custom pattern you can use DateField.setDateFormat() to force a specific format. You should be able to base that on the locale using something like:

        DateField df = new DateField("A datefield");
        Locale locale = new Locale("fi", "FI");
        df.setLocale(locale);

        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
                DateFormat.LONG, locale);
        if (dateFormat instanceof SimpleDateFormat) {
            String pattern = ((SimpleDateFormat) dateFormat).toPattern();
            df.setDateFormat(pattern);
        }

Thanks for the reply. In fact, I did something like that, but I have to set it wherever I use the DateField. I am looking for an Application-wide setting to make DateField behaves like that (just like we can do setLocale() on the Application).

Currently, I inherited DateField and created my own. The problem here is that another developer may use the vaadin DateField directly.

Hi,

Currently the recommended way of doing this kind of “default settings for a component” is to inherit the component and do the settings in its constructor. Just like you have done.

Unfortunately the problem is the one you mention, other developers on a large project may not know of the inherited component and use the default one directly. The only way to address this problem is team-wide communication…

HTH,
/Jonatan

Thanks Jonatan.
One of the objectives of mine is to reduce the “extra” communications between team members :smiley:
Anyway, I will follow your advice for now.