I have a field for a DateTimePicker called created
private DateTimePicker created;
This is bound to a DTO class which has a ZonedDateTime property called created
binder = new CollaborationBinder<>(PostmortemDto.class, userInfo);
binder.forField(created).withConverter(new ZonedDateTimeConverter());
This my implementation of ZonedDateTimeConverter
public class ZonedDateTimeConverter implements Converter<LocalDateTime, ZonedDateTime> {
private ZoneId zoneId;
/**
* Creates a new converter using the given time zone.
*
* @param zoneId the time zone to use, not <code>null</code>
*/
public ZonedDateTimeConverter(ZoneId zoneId) {
this.zoneId = Objects.requireNonNull(zoneId, "Zone identifier cannot be null");
}
public ZonedDateTimeConverter() {
this.zoneId = ZonedDateTime.now().getZone();
}
@Override
public Result<ZonedDateTime> convertToModel(LocalDateTime localDate, ValueContext context) {
if (localDate == null) {
return Result.ok(null);
}
return Result.ok(ZonedDateTime.of(localDate, zoneId));
}
@Override
public LocalDateTime convertToPresentation(ZonedDateTime zonedDateTime, ValueContext context) {
if (zonedDateTime == null) {
return null;
}
return zonedDateTime.toLocalDateTime();
}
}
However it is not working, it throws the error relating to binding and incompatible types
Caused by: java.lang.IllegalStateException: Property type ‘java.time.ZonedDateTime’ doesn’t match the field type ‘java.time.LocalDateTime’. Binding should be configured manually using converter.
at com.vaadin.flow.data.binder.Binder.bindProperty(Binder.java:3320)
at com.vaadin.flow.data.binder.Binder.lambda$bindInstanceFields$25(Binder.java:3190)
at com.vaadin.flow.data.binder.Binder.handleProperty(Binder.java:3426)
Any ideas what I have done wrong?