Patricio Jerí Patricio Jerí - 29 days ago 15
Java Question

Vaadin InlineDateFormat Resolution Steps

For Vaadin's server side InlineDateField, one can set the resolution to minutes like this:

timeField = new InlineDateField( caption );
timeField.setResolution(Resolution.MINUTE);


How do you set the step, eg: I want to get minutes in 15 minute intervals as opposed to getting a full list of 60 minutes.

https://vaadin.com/docs/-/part/framework/components/components-datefield.html

Answer

You can't do that with this component.

But you can use it with Resolution.DAY(default) and generate your own Combo boxes for hour and minute pickers.

Example component:

public class MyDatePicker extends VerticalLayout {

    private InlineDateField dateField;
    private NativeSelect hourPicker;
    private NativeSelect minutePicker;

    public MyDatePicker(String caption) {
        setSizeUndefined();

        dateField = new InlineDateField(caption);
        hourPicker = new NativeSelect();
        minutePicker = new NativeSelect();

        HorizontalLayout hourLayout = new HorizontalLayout();
        hourLayout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
        Label colonLabel = new Label("<b>:</b>");
        colonLabel.setContentMode(ContentMode.HTML);
        hourLayout.addComponents(hourPicker, colonLabel, minutePicker);

        addComponents(dateField, hourLayout);
        setComponentAlignment(hourLayout, Alignment.MIDDLE_CENTER);

        setValues();
    }

    private void setValues() {
        hourPicker.setContainerDataSource(new BeanItemContainer<>(String.class, new ArrayList<>(Arrays.asList("01", "02", "...", "24"))));
        minutePicker.setContainerDataSource(new BeanItemContainer<>(String.class, new ArrayList<>(Arrays.asList("00", "15", "30", "45"))));

        hourPicker.select("01");
        minutePicker.select("00");
    }

    public Date getValue() {
        Date date = dateField.getValue();

        // set hour and minutes to date

        return date;
    }

}