Search code examples
javavaadinvaadin7datefield

Vaadin datefield component set a default date


My question is if I put a date field in my web application's UI using vaadin framework like following codes, then how can I set a default date value to it? I can also see the setValue() method suggestion coming with date field in eclipse editor but has not much information how to set a particular date with setValue() in its documentation.

DateField dueDate = new DateField();
dueDate.setDateFormat("dd-MM-yyyy");
dueDate.setVisible(true);
dueDate.setCaption("Due Date");
**dueDate.setValue();**

Thanks in advance for your concerns. I am using vaadin7.


Solution

  • It's indeed the setValue method you need to call. You can supply a java.util.Date object. The first code example on this documentation page shows it.

    // Create a DateField with the default style
    DateField date = new DateField();
    
    // Set the date and time to present
    date.setValue(new Date());
    

    If you are on Java 8 and want to use a LocalDate or LocalDateTime, you need to convert it to java.util.Date or write a Converter for the DateField (I did that some time ago, see here).