Adding a DateTimePicker to a form ALWAYS causes the binder to report as bei

I recently added a DateTimePicker to one of my forms - not to actually pick a date or time but just to display a timestamp (LocalDateTime), i.e. as read-only field.

As I had to learn, for some odd reason this field ALWAYS causes the binder to report as being changed! I only noticed that odd behavior because my app - when leaving the form and the data has changed without saving - will ask the user whether (s)he wants to save or abandon the change(s). As soon as I added the timestamp I am ALWAYS asked, even if I changed nothing.

To reproduce:

Add a DateTime-field to one of your entities (e.g. I used the “Contact”-entity in the Vaadin CRM example application). add the below member variable:

private LocalDateTime timestamp = LocalDateTime.now();

Add a DateTimePicker-field to the corresponding form:

DateTimePicker timestamp;

Create and add the form using a read-only binding:

timestamp = new DateTimePicker("Timestamp"));
this.binder.forField(timestamp).bind(Contact::getTimestamp, null);
// dont forget to actually add the field to the form...

Add some indicator or a log-message that signals whether the binder’s hasChanges() is true or false to verify.
E.g. I simply switch the Save-button active/inactive depending on binder.hasChanges() - “save” is the Save-button:

binder.addStatusChangeListener(e -> save.setEnabled(binder.isValid() && binder.hasChanges()));

The Save-Button is ALWAYS active because “hasChanges()” always reports true. This is NOT expected and esp. NOT WANTED behavior!

I also started to investigate. The “changed”-state seems to have to do with the fact that the value of the timestamp gets truncated (or rounded?) to minutes when displayed and thus probably isn’t the same anymore when compared to the original value. As I said: this IMHO is most unwanted behavior!

That sounds like a bug to me; I’d recommend creating a ticket at https://github.com/vaadin/vaadin-date-time-picker/issues .

-Olli

OK - done. Thanks and have a good day!

Michael Moser:
OK - done. Thanks and have a good day!

You may be able to work around it by stripping seconds/milliseconds before setting the value the first time. Just a guess, I haven’t tried it.

You may be able to work around it by stripping seconds/milliseconds before setting the value the first time. Just a guess, I haven’t tried it.

Yes, it is related to

https://github.com/vaadin/vaadin-date-time-picker/issues/49

Martin Israelsen:
You may be able to work around it by stripping seconds/milliseconds before setting the value the first time. Just a guess, I haven’t tried it.

I guess - since I only use it to display a timestamp (where I actually want to see seconds and subseconds) a DateTimePicker isn’t the right solution anyway. So I will probably rather replace it with a Label oder a read-only TextField, anyway. But thanks for the suggestion, nevertheless!

Later addition: doing so I discovered Vaadin’s Converter-concept. That’s great and makes such conversions a snap!