Docs

Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Date Picker

Date Picker is an input field that allows the user to enter a date by typing or by selecting from a calendar overlay.

Open in a
new tab
Source code
DatePickerBasic.java

The date can be entered directly using the keyboard in the format of the current locale or through the date picker overlay. The overlay opens when the field is clicked and/or any input is entered when the field is focused.

Common Input Field Features

Date Picker includes all Text Field and shared input field features.

Date Format

The date picker can be configured to display dates and parse user input in a specific format.

Using Java Locales

The date picker can be configured to display dates and parse user input in a specific format. By default, the date picker displays and parses dates using the user’s locale (reference). Alternatively, setting a specific locale ensures that all users consistently see the same format.

Open in a
new tab
Source code
DatePickerLocale.java

The date format that is used based on the locale depends on the specific browser implementation and might not be reliable when expecting a specific pattern.

Validation

Min & Max Value

The valid input range of Date Picker can be restricted by defining min and max values. Dates before the min and after the max are disabled in the overlay. A helper text can be used to inform the user about the accepted range.

Open in a
new tab
Source code
DatePickerMinMax.java

Custom Validation

Date Picker supports custom validation, such as limiting the options to Monday through Friday.

Open in a
new tab
Source code
DatePickerCustomValidation.java

Week Numbers

Week numbers (ISO-8601) can be enabled in the calendar overlay. This works only when the first day of the week is set to Monday.

Open in a
new tab
Source code
DatePickerWeekNumbers.java

Initial Position

Date Picker’s initial position parameter defines which date is focused in the calendar overlay when the overlay is opened. The default initial position is the selected or current date.

Use this feature to minimise the need for unnecessary navigation and/or scrolling when the user’s input is expected to be within a certain time.

Open in a
new tab
Source code
DatePickerInitialPosition.java

Auto Open

The overlay automatically opens when the field is focused. This can be prevented, to have the overlay only open when the toggle button or Up/Down arrow keys are pressed. Note, that the behavior is not affected on touch devices.

Open in a
new tab
Source code
DatePickerAutoOpen.java

Usage Patterns

Date Range

You can create a date range picker using two Date Pickers.

Open in a
new tab
Source code
DatePickerDateRange.java

To disable the days before the start date in the end date picker, you need to handle the selection in the start date picker and change the range in the end date picker.

Best Practises

Picking vs Typing

The calendar overlay is useful when the users need to choose a day that is close to the current date or when information such as day of the week, week number, and valid dates, etc. can aid in choosing the best option.

For days well in the past or future, and for known dates such as birthdays, typing the date in the input field can be the faster and easier approach. Because of this, it is important to verify that the user can enter dates according to their locale.

Instead of a Date Picker, you can use individual input fields for day, month, and year, to improve usability on small touch devices.

Open in a
new tab
Source code
DatePickerIndividualInputFields.java
Note
Not production-ready

The above example is only a prototype implementation to demonstrate the idea, and not ready for production use.

Show the Date Format

Use a placeholder or helper to show how the input should be formatted. For example, 12/6/2020 represents different dates for Americans and Europeans.

Open in a
new tab
Source code
DatePickerDateFormatIndicator.java

Helpers are preferable to placeholders, as they are always visible. Fields with placeholders are also less noticeable than empty fields and susceptible to being skipped. Use placeholders when space is limited, for example, when Date Picker is used as a filter in a data grid header.

Component Usage recommendations

Time Picker

Input field for entering or selecting a specific time.

Date Time Picker

Input field for selecting both a date and a time.

D3BFA709-7879-4C25-9835-35944915A3D0