java-8vaadin8

vaadin8 DateField: Cannot cast java.time.LocalDate to java.util.Date


I have simple entity (for example)

 import java.util.Date;
 class People implements Serializable{
   ...
   private Date birthdate; //(getters, setters)
   ...}

UI code:

 final Binder<People> binder = new Binder<People>(People.class);  ...
 People bean=new  People();
 binder.setBean(bean);
 DateField birthdate = new DateField("date of birth");
 binder.bind(birthdate, "birthdate");

When I select date from calendar in UI I get:

 Caused by: java.lang.ClassCastException: Cannot cast java.time.LocalDate to java.util.Date
at java.lang.Class.cast(Class.java:3369)
at com.vaadin.data.Binder$BindingBuilderImpl.lambda$createConverter$f6099586$1(Binder.java:800)
at com.vaadin.data.Converter.lambda$null$fdd4de71$1(Converter.java:105)
at com.vaadin.data.Result.of(Result.java:91)

I tried to use

 DateField birthdate = new DateField("birthdate");
 binder.bind(birthdate, "birthdate");
 binder.forField(birthdate).withConverter(new LocalDateToDateConverter());

but have same result. How to bind Date to DateField properly?


Solution

  • The problem is how you make use of the binder. Instead try

    DateField birthdate = new DateField("birthdate");
    binder.forField(birthdate).withConverter(new LocalDateToDateConverter()).bind("birthdate");
    

    The forField method returns an object following the builder design pattern. That means you call some (chained) methods on that object and finish it by a call to bind.