vaadin / vaadin/framework

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

Open
#10,502 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
1.8k
Forks
717
Avg merge
2d 6h
Merged PRs (30d)
3

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the DateField behavior with the "dd.MM.yyyy" format and the input "08.01.18", then trace the component's date parsing entry point. Check how two-digit years are interpreted when a four-digit format is configured. Done means the input no longer becomes year 0018 and existing date parsing behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.