@Tag(value="vaadin-date-time-picker") @NpmPackage(value="@vaadin/vaadin-date-time-picker", version="2.1.0") @JsModule(value="@vaadin/vaadin-date-time-picker/src/vaadin-date-time-picker.js") public class DateTimePicker extends AbstractSinglePropertyField<DateTimePicker,LocalDateTime> implements HasStyle, HasSize, HasTheme, HasValidation, Focusable<DateTimePicker>, HasHelper, HasLabel, HasValidator<LocalDateTime>, HasClientValidation
vaadin-date-time-picker web component.AbstractField.ComponentValueChangeEvent<C extends Component,V>BlurNotifier.BlurEvent<C extends Component>FocusNotifier.FocusEvent<C extends Component>HasClientValidation.ClientValidatedEventHasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>| Constructor and Description |
|---|
DateTimePicker()
Default constructor.
|
DateTimePicker(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
Convenience constructor to create a date time picker with a
ValueChangeListener. |
DateTimePicker(LocalDateTime initialDateTime)
Convenience constructor to create a date time picker with a pre-selected
date time in current UI locale format.
|
DateTimePicker(LocalDateTime initialDateTime,
HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
Convenience constructor to create a date time picker with a pre-selected
date time in current UI locale format and a
ValueChangeListener. |
DateTimePicker(LocalDateTime initialDateTime,
Locale locale)
Convenience constructor to create a date time picker with pre-selected
date time and locale setup.
|
DateTimePicker(String label)
Convenience constructor to create a date time picker with a label.
|
DateTimePicker(String label,
HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
Convenience constructor to create a date time picker with a
ValueChangeListener and a label. |
DateTimePicker(String label,
LocalDateTime initialDateTime)
Convenience constructor to create a date time picker with a pre-selected
date and time in current UI locale format and a label.
|
DateTimePicker(String label,
LocalDateTime initialDateTime,
HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
Convenience constructor to create a date time picker with a pre-selected
date and time in current UI locale format, a
ValueChangeListener
and a label. |
| Modifier and Type | Method and Description |
|---|---|
void |
addThemeName(String themeName)
Adds a theme name to this component.
|
void |
addThemeNames(String... themeNames)
Adds one or more theme names to this component.
|
Registration |
addValidationStatusChangeListener(ValidationStatusChangeListener<LocalDateTime> listener)
Enables the implementing components to notify changes in their validation
status to the observers.
|
DatePicker.DatePickerI18n |
getDatePickerI18n()
Gets the internationalization object previously set for this component.
|
String |
getDatePlaceholder()
Gets the placeholder string of the date field.
|
Validator<LocalDateTime> |
getDefaultValidator()
Returns a validator that checks the state of the Value.
|
String |
getErrorMessage()
Gets the error message to display when the input is invalid.
|
String |
getLabel()
Gets the label of this field.
|
Locale |
getLocale()
Gets the Locale for this DateTimePicker
|
LocalDateTime |
getMax()
Gets the maximum date and time in the date time picker.
|
LocalDateTime |
getMin()
Gets the minimum date and time in the date time picker.
|
Duration |
getStep()
Gets the step of the time picker.
|
String |
getTimePlaceholder()
Gets the placeholder string of the time field.
|
boolean |
isAutoOpen()
When auto open is enabled, the dropdown will open when the field is
clicked.
|
protected boolean |
isEnforcedFieldValidationEnabled()
Whether the full experience validation is enforced for the component.
|
boolean |
isInvalid()
Gets the validity indication of the date time picker output.
|
boolean |
isWeekNumbersVisible()
Get the state of
showWeekNumbers property of the date picker. |
protected void |
onAttach(AttachEvent attachEvent)
Called when the component is attached to a UI.
|
boolean |
removeThemeName(String themeName)
Removes a theme name from this component.
|
void |
removeThemeNames(String... themeNames)
Removes one or more theme names from component.
|
void |
setAutoOpen(boolean autoOpen)
When auto open is enabled, the dropdown will open when the field is
clicked.
|
void |
setDatePickerI18n(DatePicker.DatePickerI18n i18n)
Sets the internationalization properties for the date picker inside this
component.
|
void |
setDatePlaceholder(String placeholder)
Sets a placeholder string for the date field.
|
void |
setErrorMessage(String errorMessage)
Sets the error message to display when the input is invalid.
|
void |
setInvalid(boolean invalid)
Sets the validity indication of the date time picker output.
|
void |
setLabel(String label)
Sets the label for this field.
|
void |
setLocale(Locale locale)
Set the Locale for the DateTimePicker.
|
void |
setMax(LocalDateTime max)
Sets the maximum date and time in the date time picker.
|
void |
setMin(LocalDateTime min)
Sets the minimum date and time in the date time picker.
|
void |
setReadOnly(boolean readOnly)
Sets the read-only mode of this
HasValue to given mode. |
void |
setRequiredIndicatorVisible(boolean requiredIndicatorVisible)
Sets whether the date time picker is marked as input required.
|
void |
setStep(Duration step)
Sets the
step property of the time picker using duration. |
void |
setThemeName(String themeName)
Sets the theme names of this component.
|
void |
setThemeName(String themeName,
boolean set)
Sets or removes the given theme name for this component.
|
void |
setTimePlaceholder(String placeholder)
Set a placeholder string for the time field.
|
void |
setValue(LocalDateTime value)
Sets the selected date and time value of the component.
|
void |
setWeekNumbersVisible(boolean weekNumbersVisible)
Show or hide the week numbers in the date picker.
|
protected void |
validate()
Performs server-side validation of the current value.
|
getSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEventaddValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, valueEqualsaddListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, set, setElement, setId, setVisibleclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNamegetCssSize, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullgetThemeName, getThemeNames, hasThemeNameaddFocusShortcut, blur, focus, getTabIndex, setTabIndexaddBlurListeneraddFocusListenerisEnabled, setEnabledgetElementgetHelperComponent, getHelperText, setHelperComponent, setHelperTextaddClientValidatedEventListenerisReadOnly, isRequiredIndicatorVisibleclear, getOptionalValueaddAttachListeneraddDetachListenerpublic DateTimePicker()
public DateTimePicker(String label)
label - the label describing the date time pickersetLabel(String)public DateTimePicker(String label, LocalDateTime initialDateTime)
label - the label describing the date time pickerinitialDateTime - the pre-selected date time in the pickersetValue(LocalDateTime),
setLabel(String)public DateTimePicker(LocalDateTime initialDateTime)
initialDateTime - the pre-selected date time in the pickerpublic DateTimePicker(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
ValueChangeListener.listener - the listener to receive value change eventsAbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DateTimePicker(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
ValueChangeListener and a label.label - the label describing the date time pickerlistener - the listener to receive value change eventssetLabel(String),
AbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DateTimePicker(LocalDateTime initialDateTime, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
ValueChangeListener.initialDateTime - the pre-selected date time in the pickerlistener - the listener to receive value change eventssetValue(LocalDateTime),
AbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DateTimePicker(String label, LocalDateTime initialDateTime, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>> listener)
ValueChangeListener
and a label.label - the label describing the date time pickerinitialDateTime - the pre-selected date time in the pickerlistener - the listener to receive value change eventssetLabel(String),
setValue(LocalDateTime),
AbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DateTimePicker(LocalDateTime initialDateTime, Locale locale)
initialDateTime - the pre-selected date time in the pickerlocale - the locale for the date time pickerpublic void setValue(LocalDateTime value)
The value will be truncated to millisecond precision, as that is the
maximum that the time picker supports. This means that
AbstractField.getValue() might return a different value than what was passed
in.
setValue in interface HasValue<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>,LocalDateTime>setValue in class AbstractField<DateTimePicker,LocalDateTime>value - the LocalDateTime instance representing the selected date and
time, or nullpublic void setReadOnly(boolean readOnly)
HasValueHasValue to given mode. The user
can't change the value when in read-only mode.
A HasValue with a visual component in read-only mode typically
looks visually different to signal to the user that the value cannot be
edited.
setReadOnly in interface HasValue<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>,LocalDateTime>setReadOnly in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>,LocalDateTime>readOnly - a boolean value specifying whether the component is put
read-only mode or notpublic void setLabel(String label)
public String getLabel()
public void setDatePlaceholder(String placeholder)
placeholder - the String value to setpublic String getDatePlaceholder()
placeholder property of the date pickerpublic void setTimePlaceholder(String placeholder)
placeholder - the String value to setpublic String getTimePlaceholder()
placeholder property of the time pickerpublic void setStep(Duration step)
step property of the time picker using duration. It
specifies the intervals for the displayed items in the time picker
dropdown and also the displayed time format.
The set step needs to evenly divide a day or an hour and has to be larger
than 0 milliseconds. By default, the format is hh:mm (same as *
Duration.ofHours(1)
If the step is less than 60 seconds, the format will be changed to
hh:mm:ss and it can be in hh:mm:ss.fff format, when the
step is less than 1 second.
NOTE: If the step is less than 900 seconds, the dropdown is hidden.
NOTE: changing the step to a larger duration can cause a new
HasValue.ValueChangeEvent to be fired
if some parts (eg. seconds) is discarded from the value.
step - the step to set, not null and should divide a day or
an hour evenlypublic Duration getStep()
step property from the picker, unit secondspublic void setWeekNumbersVisible(boolean weekNumbersVisible)
Set true to display ISO-8601 week numbers in the calendar.
Note that displaying of week numbers is only supported when i18n.firstDayOfWeek is 1 (Monday).
weekNumbersVisible - the boolean value to set#setDatePickerI18n(DatePickerI18n),
DatePicker.DatePickerI18n.setFirstDayOfWeek(int)public boolean isWeekNumbersVisible()
showWeekNumbers property of the date picker.showWeekNumbers property from the date pickerpublic void setLocale(Locale locale)
locale - the locale to set to the DateTimePicker, cannot be nullpublic Locale getLocale()
public void addThemeName(String themeName)
addThemeName in interface HasThemethemeName - the theme name to add, not nullpublic boolean removeThemeName(String themeName)
removeThemeName in interface HasThemethemeName - the theme name to remove, not nulltrue if the theme name was removed,
false if the theme list didn't contain the theme
namepublic void setThemeName(String themeName)
setThemeName in interface HasThemethemeName - a space-separated string of theme names to set, or empty
string to remove all theme namespublic void setThemeName(String themeName, boolean set)
setThemeName in interface HasThemethemeName - the theme name to set or remove, not nullset - true to set the theme name, false to
remove itpublic void addThemeNames(String... themeNames)
addThemeNames in interface HasThemethemeNames - the theme name or theme names to be added to the componentpublic void removeThemeNames(String... themeNames)
removeThemeNames in interface HasThemethemeNames - the theme name or theme names to be removed from the componentpublic void setErrorMessage(String errorMessage)
setErrorMessage in interface HasValidationerrorMessage - a new error messagepublic String getErrorMessage()
getErrorMessage in interface HasValidationpublic void setInvalid(boolean invalid)
setInvalid in interface HasValidationinvalid - new value for component input validitypublic boolean isInvalid()
isInvalid in interface HasValidationpublic Validator<LocalDateTime> getDefaultValidator()
HasValidatorBinder and
can be seen as a validation failure.getDefaultValidator in interface HasValidator<LocalDateTime>public Registration addValidationStatusChangeListener(ValidationStatusChangeListener<LocalDateTime> listener)
HasValidator
Note: This method can be overridden by the implementing
classes e.g. components, to enable the associated Binder.Binding
instance subscribing for their validation change events and revalidate
itself.
This method primarily designed for notifying the Binding about the
validation status changes of a bound component at the client-side.
WebComponents such as <vaadin-date-picker> or any
other component that accept a formatted text as input should be able to
communicate their invalid status to their server-side instance, and a
bound server-side component instance must notify its binding about this
validation status change as well. When the binding instance revalidates,
a chain of validators and convertors get executed one of which is the
default validator provided by HasValidator.getDefaultValidator().
Thus, In order for the binding to be able to show/clear errors for its
associated bound field, it is important that implementing components take
that validation status into account while implementing any validator and
converter including HasValidator.getDefaultValidator(). Here is
an example:
@Tag("date-picker-demo")
public class DatePickerDemo implements HasValidator<LocalDate> {
// Each web component has a way to communicate its validation status
// to its server-side component instance. The following clientSideValid
// state is introduced here just for the sake of simplicity of this code
// snippet:
boolean clientSideValid = true;
/**
* Note how clientSideValid engaged in the definition
* of this method. It is important to reflect this status either
* in the returning validation result of this method or any other
* validation that is associated with this component.
*/
@Override
public Validator getDefaultValidator() {
return (value, valueContext) -> clientSideValid ? ValidationResult.ok()
: ValidationResult.error("Invalid date format");
}
private final Collection<ValidationStatusChangeListener<LocalDate>>
validationStatusListeners = new ArrayList<>();
/**
* This enables the binding to subscribe for the validation status
* change events that are fired by this component and revalidate
* itself respectively.
*/
@Override
public Registration addValidationStatusChangeListener(
ValidationStatusChangeListener<LocalDate> listener) {
validationStatusListeners.add(listener);
return () -> validationStatusListeners.remove(listener);
}
private void fireValidationStatusChangeEvent(
boolean newValidationStatus) {
if (this.clientSideValid != newValidationStatus) {
this.clientSideValid = newValidationStatus;
var event = new ValidationStatusChangeEvent<>(this,
newValidationStatus);
validationStatusListeners.forEach(
listener -> listener.validationStatusChanged(event));
}
}
}
addValidationStatusChangeListener in interface HasValidator<LocalDateTime>Binder.BindingBuilderImpl.bind(ValueProvider,
Setter)protected void validate()
public void setMin(LocalDateTime min)
min - the minimum date and time that is allowed to be set, or
null to remove any minimum constraintspublic LocalDateTime getMin()
null if there's no minimumpublic void setMax(LocalDateTime max)
max - the maximum date and time that is allowed to be set, or
null to remove any minimum constraintspublic LocalDateTime getMax()
null if there's no minimumpublic DatePicker.DatePickerI18n getDatePickerI18n()
DateTimePicker#setDatePickerI18n(DatePickerI18n)null, If the i18n
properties weren't set.public void setDatePickerI18n(DatePicker.DatePickerI18n i18n)
i18n - the internationalized properties, not nullpublic void setRequiredIndicatorVisible(boolean requiredIndicatorVisible)
setRequiredIndicatorVisible in interface HasValue<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>,LocalDateTime>setRequiredIndicatorVisible in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<DateTimePicker,LocalDateTime>,LocalDateTime>requiredIndicatorVisible - the value of the requiredIndicatorVisible to be setpublic void setAutoOpen(boolean autoOpen)
autoOpen - Value for the auto open property,public boolean isAutoOpen()
true if auto open is enabled. false otherwise.
Default is trueprotected void onAttach(AttachEvent attachEvent)
ComponentThe default implementation does nothing.
This method is invoked before the AttachEvent is fired for the
component.
protected boolean isEnforcedFieldValidationEnabled()
Exposed with protected visibility to support mocking
The method requires the VaadinSession instance to obtain the
application configuration properties, otherwise, the feature is
considered disabled.
true if enabled, false otherwise.Copyright © 2025. All rights reserved.