DateField: Format "dd.MM.yyyy" returns "08.01.0018" when entering a 2-digit year
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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