I want to have PopupDataField for changing the date in my form, and separatly changing hours and minutes by NativeSelect components. So then I want to have some date which has year, month and day from PopupDataField and time from those NativeSelects. I dont know how to save this "date" bc when i call
popupDateField.getValue()
I'm reciving whole data with hours and minutes, often different from this on NativeSelects.
I was trying to disable saving the hour by
setResolution(Resolution.DAY);
but it's not working.
I dont know how to do this, can someone help me?
I want to have some field data- where ll be year, month day from DateField and hour, minutes from NativeSelects
PopupDateField will always return Date object as its value hence you will receive from this component hours and minutes. The solution is simple - just ignore them. Construct your Date
or LocalDate object from the all components not just PopupDateField
.
@Override
protected void init(VaadinRequest request) {
HorizontalLayout layout = new HorizontalLayout();
PopupDateField popupDateField = new PopupDateField();
NativeSelect hoursSelect = new NativeSelect("Hours");
for(int i=0; i<24; i++){
hoursSelect.addItem(i);
}
NativeSelect minutesSelect = new NativeSelect("Minutes");
for(int i=0; i<60; i++){
minutesSelect.addItem(i);
}
class MyValueChangeListener implements ValueChangeListener{
public void valueChange(ValueChangeEvent event) {
LocalDate date = popupDateField.getValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int year = date.getYear();
int month = date.getMonth().getValue();
int days = date.getDayOfMonth();
int minutes = minutesSelect.getValue() == null ? 0 : (int) minutesSelect.getValue();
int hours = hoursSelect.getValue() == null ? 0 : (int) hoursSelect.getValue();
System.out.println(LocalDateTime.of(year, month, days, hours, minutes));
}
}
ValueChangeListener listener = new MyValueChangeListener();
minutesSelect.addValueChangeListener(listener);
hoursSelect.addValueChangeListener(listener);
popupDateField.addValueChangeListener(listener);
setContent(layout);
layout.addComponent(popupDateField);
layout.addComponent(hoursSelect);
layout.addComponent(minutesSelect);
}
You can also extend PopupDateField
class by adding getLocalDate
method which will convert Date
to LocalDate
.