Home > Enterprise >  Vaadin DateTimePicker becomes null if only one field (date or time field) is entered
Vaadin DateTimePicker becomes null if only one field (date or time field) is entered

Time:01-18

I have vaadin DateTimePicker component and I am using it to filter some datas. If users entered only one of date or time field, DateTimePicker component's value becomes null. I want to get date and time field seperately. Actually, I need to set time field to default value such as 12:00AM, If user entered only date field. However I couldnt find which field is null and the value of field which is not null.

I've tried to assign it's value to LocalDateTime parameter but whenever I use getValue() method for DateTimePicker which has one null field, the value returns as null.

private DateTimePicker start = new DateTimePicker();
        private boolean isTimeNull(){
            LocalDateTime dateTime = start.getValue();
        }

CodePudding user response:

Default value for the other field (usually Time) is a missing feature in the component. But you can workaround that with JavaScript.

https://github.com/vaadin/web-components/issues/668

Here is example how to pad time with zero hours if missing.

  DateTimePicker picker = new DateTimePicker();
 
  picker.getElement().executeJs("this.getElementsByTagName(\"vaadin-date-time-picker-date-picker\")[0].addEventListener('change', function(){this.getElementsByTagName(\"vaadin-date-time-picker-time-picker\")[0].value='00:00';}.bind(this));");
  picker.addValueChangeListener(event -> {      
      Notification.show(event.getValue().toString());
  });
  add(picker);
  • Related