Binding a DateField to a form

Hi,
I bind a DateField to a form in this way:

DateField t=new DateField("Date");
t.setResolution(DateResolution.DAY);
Binder myBinder=new Binder<>(mioBean.getClass());
BindingBuilder builder=myBinder.forField(t)
		.withNullRepresentation("");
builder.bind(fieldName);

When I read the bean from database and send to the form with setBean(myBean), I get:

java.lang.String cannot be cast to java.time.temporal.Temporal

I also tryed adding

.withConverter(new StringToDateConverter()) 

with the same error. I couldn’t find any documentation about this. What is the right way to bind DateFields?
Thanks, Francesco

What is the type of the attribute you try to bind at builder.bind(fieldName)? Tried your example with a simple bean that has a attribute LocalDate date; and it worked well.

If the type of your attribute is String, change it to LocalDate, then it should work. The converter is not needed.

Hi,
thank you for your answer. The type is Date. In the database (postgresql) is mapped as timestamp.
Francesco

Which version of Vaadin do you use (in current version 8.3.3 a LocalDate is expected)?

Version 8.3.2

Hi,

LocalDate is different than Date. DateField value type is LocalDate. If your bean has a Date attribute you need to converter it.

For debugging, you can use getter and setter instead of fieldname:
Instead of builder.bind(fieldName); use builder.bind(MyBean::getDate, MyBean::setDate)

Then you should have a error in your IDE (you won’t need to run it)

All my beans have Date properties and not LocalDate. How can I bind them to DateField? Is there a Converter?

I think there is no converter for that (I’m not sure). You have to write it.

I tried but without success. Which type of converter do I need?

Does this LocalDateToDateConverter work?
Basically you have a DateField returns you a LocalDate, in the bean, you have a Date, so most likely you would need a LocalDateToDateConverter

For Date use LocalDate and LocalDateTime then you dont need any converter. If you get string from datebase then you need use converter.

binder.forField(fieldName).bind("propertyName")

I use DateField and LocalDate and no problem

DateField dateBorn = new DateField(bundle.getString("label.birthdate"));
		dateBorn.setDateFormat(browserLocale == Locale.forLanguageTag("en") ? "dd.MM.yyyy" : null);
		dateBorn.setRangeEnd(LocalDate.now());
		dateBorn.setValue(LocalDate.now());
		
binder.forField(dateBorn).asRequired().bind(Person::getDate, Person::setDate);