vaadin / framework

Vaadin 6, 7, 8 is a Java framework for modern Java web applications.
http://vaadin.com/
Other
1.78k stars 729 forks source link

DateField: Format "dd.MM.yyyy" returns "08.01.0018" when entering a 2-digit year #10502

Open m-escher opened 6 years ago

m-escher commented 6 years ago

When the Dateformat of a DateField-Component is set to "dd.MM.yyyy" any input String with a 2-digit year is parsed with leading zeroes: "08.01.18" -> " "08.01.0018".

After chatting with one of the Vaadin Experts (Olli) he suggested following workaround (Vaadin 7.7.7):

dateField.addValueChangeListener((Property.ValueChangeListener) this::_validateBeforeSave);
  private void _validateBeforeSave(Property.ValueChangeEvent pEvent)
  {
    PopupDateField dateField = (PopupDateField) pEvent.getProperty();

    Calendar unvalidatedValue = Calendar.getInstance();
    unvalidatedValue.setTime(dateField.getValue());

    if (unvalidatedValue.get(Calendar.YEAR) < 100)
    {
      Calendar currentDate = Calendar.getInstance();
      currentDate.setTime(new Date());
      unvalidatedValue.add(Calendar.YEAR, (currentDate.get(Calendar.YEAR) / 100) * 100);
      dateField.setValue(unvalidatedValue.getTime());
    }
  }

This fix seems to work just fine but isn't quite the cleanest solution. Maybe there could be some out of the box Solution for handling 2-digit years within a 4-digit year Format.

Here are some other links regarding this behaviour: https://stackoverflow.com/questions/5143763/is-it-possible-to-create-a-dateformatter-which-converts-a-two-digit-year-into-a https://github.com/vaadin/framework/issues/6083

Merowing3r commented 5 years ago

I have the same issue. But for me it would make sense to "autocomplete" it to 20xx if only 2 digits are entered, and for some usecases, where e.g. 0018 is entered and needed, it must not be transformed to 2018.

What i need to know is, what exactly entered the user? 01.01.18 OR 01.01.0018 ? Why can't i access newDateString from update() - rpc in AbstractDateField somehow or Override doSetValue with the real user input (not already processed date)?