Class DateTimePicker

    • Method Detail

      • setLabel

        public void setLabel​(String label)
        Sets the label for this field.
        Specified by:
        setLabel in interface HasLabel
        Parameters:
        label - the String value to set
      • getLabel

        public String getLabel()
        Gets the label of this field.
        Specified by:
        getLabel in interface HasLabel
        Returns:
        the label property of the date time picker
      • setDatePlaceholder

        public void setDatePlaceholder​(String placeholder)
        Sets a placeholder string for the date field.
        Parameters:
        placeholder - the String value to set
      • getDatePlaceholder

        public String getDatePlaceholder()
        Gets the placeholder string of the date field.
        Returns:
        the placeholder property of the date picker
      • setTimePlaceholder

        public void setTimePlaceholder​(String placeholder)
        Set a placeholder string for the time field.
        Parameters:
        placeholder - the String value to set
      • getTimePlaceholder

        public String getTimePlaceholder()
        Gets the placeholder string of the time field.
        Returns:
        the placeholder property of the time picker
      • setStep

        public void setStep​(Duration step)
        Sets the 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.

        Parameters:
        step - the step to set, not null and should divide a day or an hour evenly
      • getStep

        public Duration getStep()
        Gets the step of the time picker.
        Returns:
        the step property from the picker, unit seconds
      • setWeekNumbersVisible

        public void setWeekNumbersVisible​(boolean weekNumbersVisible)
        Show or hide the week numbers in the date picker. By default the week numbers are not shown.

        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).

        Parameters:
        weekNumbersVisible - the boolean value to set
        See Also:
        setDatePickerI18n(DatePickerI18n), DatePicker.DatePickerI18n.setFirstDayOfWeek(int)
      • isWeekNumbersVisible

        public boolean isWeekNumbersVisible()
        Get the state of showWeekNumbers property of the date picker.
        Returns:
        the showWeekNumbers property from the date picker
      • setLocale

        public void setLocale​(Locale locale)
        Set the Locale for the DateTimePicker. The displayed date and time will be matched to the format used in that locale.
        Parameters:
        locale - the locale to set to the DateTimePicker, cannot be null
      • getLocale

        public Locale getLocale()
        Gets the Locale for this DateTimePicker
        Overrides:
        getLocale in class Component
        Returns:
        the locale used for this DateTimePicker
      • addThemeName

        public void addThemeName​(String themeName)
        Adds a theme name to this component.
        Specified by:
        addThemeName in interface HasTheme
        Parameters:
        themeName - the theme name to add, not null
      • removeThemeName

        public boolean removeThemeName​(String themeName)
        Removes a theme name from this component.
        Specified by:
        removeThemeName in interface HasTheme
        Parameters:
        themeName - the theme name to remove, not null
        Returns:
        true if the theme name was removed, false if the theme list didn't contain the theme name
      • setThemeName

        public void setThemeName​(String themeName)
        Sets the theme names of this component. This method overwrites any previous set theme names.
        Specified by:
        setThemeName in interface HasTheme
        Parameters:
        themeName - a space-separated string of theme names to set, or empty string to remove all theme names
      • setThemeName

        public void setThemeName​(String themeName,
                                 boolean set)
        Sets or removes the given theme name for this component.
        Specified by:
        setThemeName in interface HasTheme
        Parameters:
        themeName - the theme name to set or remove, not null
        set - true to set the theme name, false to remove it
      • addThemeNames

        public void addThemeNames​(String... themeNames)
        Adds one or more theme names to this component. Multiple theme names can be specified by using multiple parameters.
        Specified by:
        addThemeNames in interface HasTheme
        Parameters:
        themeNames - the theme name or theme names to be added to the component
      • removeThemeNames

        public void removeThemeNames​(String... themeNames)
        Removes one or more theme names from component. Multiple theme names can be specified by using multiple parameters.
        Specified by:
        removeThemeNames in interface HasTheme
        Parameters:
        themeNames - the theme name or theme names to be removed from the component
      • setErrorMessage

        public void setErrorMessage​(String errorMessage)
        Sets the error message to display when the input is invalid.
        Specified by:
        setErrorMessage in interface HasValidation
        Parameters:
        errorMessage - a new error message
      • getErrorMessage

        public String getErrorMessage()
        Gets the error message to display when the input is invalid.
        Specified by:
        getErrorMessage in interface HasValidation
        Returns:
        the current error message
      • setInvalid

        public void setInvalid​(boolean invalid)
        Sets the validity indication of the date time picker output.
        Specified by:
        setInvalid in interface HasValidation
        Parameters:
        invalid - new value for component input validity
      • isInvalid

        public boolean isInvalid()
        Gets the validity indication of the date time picker output.
        Specified by:
        isInvalid in interface HasValidation
        Returns:
        the current validity indication.
      • getDefaultValidator

        public Validator<LocalDateTime> getDefaultValidator()
        Description copied from interface: HasValidator
        Returns a validator that checks the state of the Value. This should be overridden for components with internal value conversion or validation, e.g. when the user is providing a string that has to be parsed into a date. An invalid input from user will be exposed to a Binder and can be seen as a validation failure.
        Specified by:
        getDefaultValidator in interface HasValidator<LocalDateTime>
        Returns:
        state validator
      • addValidationStatusChangeListener

        public Registration addValidationStatusChangeListener​(ValidationStatusChangeListener<LocalDateTime> listener)
        Description copied from interface: HasValidator
        Enables the implementing components to notify changes in their validation status to the observers.

        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));
                 }
             }
         }
         
        Specified by:
        addValidationStatusChangeListener in interface HasValidator<LocalDateTime>
        Returns:
        Registration of the added listener.
        See Also:
        Binder.BindingBuilderImpl.bind(ValueProvider, Setter)
      • validate

        protected void validate()
        Performs server-side validation of the current value. This is needed because it is possible to circumvent the client-side validation constraints using browser development tools.
      • setMin

        public void setMin​(LocalDateTime min)
        Sets the minimum date and time in the date time picker. Dates and times before that will be disabled in the popups.
        Parameters:
        min - the minimum date and time that is allowed to be set, or null to remove any minimum constraints
      • getMin

        public LocalDateTime getMin()
        Gets the minimum date and time in the date time picker. Dates and times before that will be disabled in the popups.
        Returns:
        the minimum date and time that is allowed to be set, or null if there's no minimum
      • setMax

        public void setMax​(LocalDateTime max)
        Sets the maximum date and time in the date time picker. Dates and times above that will be disabled in the popups.
        Parameters:
        max - the maximum date and time that is allowed to be set, or null to remove any minimum constraints
      • getMax

        public LocalDateTime getMax()
        Gets the maximum date and time in the date time picker. Dates and times above that will be disabled in the popups.
        Returns:
        the maximum date and time that is allowed to be set, or null if there's no minimum
      • getDatePickerI18n

        public DatePicker.DatePickerI18n getDatePickerI18n()
        Gets the internationalization object previously set for this component. Note: updating the object content that is gotten from this method will not update the lang on the component if not set back using setDatePickerI18n(DatePickerI18n)
        Returns:
        the i18n object. It will be null, If the i18n properties weren't set.
      • setDatePickerI18n

        public void setDatePickerI18n​(DatePicker.DatePickerI18n i18n)
        Sets the internationalization properties for the date picker inside this component.
        Parameters:
        i18n - the internationalized properties, not null
      • setAutoOpen

        public void setAutoOpen​(boolean autoOpen)
        When auto open is enabled, the dropdown will open when the field is clicked.
        Parameters:
        autoOpen - Value for the auto open property,
      • isAutoOpen

        public boolean isAutoOpen()
        When auto open is enabled, the dropdown will open when the field is clicked.
        Returns:
        true if auto open is enabled. false otherwise. Default is true
      • onAttach

        protected void onAttach​(AttachEvent attachEvent)
        Description copied from class: Component
        Called when the component is attached to a UI.

        The default implementation does nothing.

        This method is invoked before the AttachEvent is fired for the component.

        Overrides:
        onAttach in class Component
        Parameters:
        attachEvent - the attach event
      • addThemeVariants

        public void addThemeVariants​(DateTimePickerVariant... variants)
        Adds theme variants to the component.
        Parameters:
        variants - theme variants to add
      • removeThemeVariants

        public void removeThemeVariants​(DateTimePickerVariant... variants)
        Removes theme variants from the component.
        Parameters:
        variants - theme variants to remove
      • isFeatureFlagEnabled

        protected boolean isFeatureFlagEnabled​(Feature feature)
        Returns true if the given feature flag is enabled, false otherwise.

        Exposed with protected visibility to support mocking

        The method requires the VaadinService instance to obtain the available feature flags, otherwise, the feature is considered disabled.

        Parameters:
        feature - the feature flag.
        Returns:
        whether the feature flag is enabled.