DateField should not displaying Time part if datatype does not allow time

Hi

When using a DateField not connected to an entity attribute, with a datatype not allowing time (e.g LocalDate) the time field part still appears.

<dateField caption="Position en Date" id="positionDateField" datatype="localDate"/>

image

Looking at the list of datatypes in the documentation, only dateTime, LocalDateTime and offsetDateFime should make the time part field appear.

  • If the field is not connected to an entity attribute (i.e. the data container and attribute name are not set), you can set the data type using the datatype attribute. DateField uses the following data types:

    • date
    • dateTime
    • localDate
    • localDateTime
    • offsetDateTime

Worse, the documentation says that to make the time part disappear you need to set the dateFormat attribute for the field with a pattern not using time.

Defining a date format for the field in screen xml or controller code means bypassing user locale, which is not convenient.

You can change the date and time format using the dateFormat attribute. An attribute value can be either a format string itself or a key in a message pack (if the value starts with msg://).

The format is defined by rules of the SimpleDateFormat class (SimpleDateFormat (Java Platform SE 8 )). If there are no H or h characters in the format, the time field will not be displayed.

It seems natural that the DateField use by default the dateFormat defined for the current locale. This is what I have done in each screen as a workaround.

image

EDIT : workaround code, it might help others facing the same issue. Can be put in a screen mixin.


    @Subscribe
    default void initScreenDefaults(Screen.InitEvent event) {
        // fix date fields format to get rid of unnecessary time part
        DatatypeRegistry reg = getBeanLocator().get(DatatypeRegistry.class);
        Locale locale = getBeanLocator().get(UserSessionSource.class).getUserSession().getLocale();
        FormatStrings formatStrings = Datatypes.getFormatStrings(locale);
        if (formatStrings == null)
            throw new DevelopmentException("unable to get FormatStrings for Locale : " + locale);
        getWindow().getComponents().stream().filter(e -> e instanceof DateField)
                .map(e -> (DateField<?>) e)
                .forEach(dateField -> {
                    Datatype<?> dt = dateField.getDatatype();
                    if (reg.get("date").equals(dt) || reg.get("localDate").equals(dt))
                        dateField.setDateFormat(formatStrings.getDateFormat());
                });
    }

Regards
Michael