I really often have database/VO/DTO elements with java.time.Instant attributes as it is the most convenient way to handle time with the Java Time API.
In the UI I only have the DateTimePicker component available, that has a locale, but no timezone information. This means a conversion is always needed like
Mapping java.time.Instant is a reasonable use case and I don’t see a conceptional difference between a LocalDateTime (where you need to find out the users timezone by yourself) and an Instant (with a fixed zone of UTC).
What are your thoughts on this? Help for easier mapping also appreciated as I have no idea how to enhance the existing class DateTimePicker extends AbstractSinglePropertyField<DateTimePicker, LocalDateTime>.
Why aren’t you just simply adding LocalDateTimeToInstantConverterto your binder and call it a day? Changing the field is going to be a maintenance nightmare.
Instant is usually not a good format for getting dates from users. It might work in simple cases but can quickly turn into a nightmare when it comes to e.g. DST and different users in different time zones.
Instant is mostly appropriate for “now” or moments in time relative to that. I would suggest using using either LocalDateTime or ZonedDateTime in DTOs and the database instead.
LocalDateTime is the appropriate type for a UI control where the user chooses the individual date coordinates from a calendar and clock. Instant is the appropriate type for a UI control where the user chooses an offset from the current time, e.g. “30 minutes ago”.
@knoobie TRUE! Too easy. I will go that approach with the converter. Thanks.
@Leif I agree that Instant is normally not a good format to get time from a user. On the other hand the LocalDateTime as used by Vaadin is missing the timezone. This means interpretation by the software is needed in which timezone the user wanted to made the entry. This information is currently not available in the DateTimePicker component. As you said, in the database I need to store Instant or ZonedDateTime in order to have it independent of user’s or systems’ timezone. So, is it possible to use the DateTimePicker with a ZonedDateTime as value and how to set the timezone then? For my use case, I can use a label like “Time in UTC” and use a converter, but what is from a UI/UX perspective the recommended way to “synchronize” the timezone with the user?
The UI component doesn’t ask the user about the time zone and it wouldn’t be reasonable for it to guess what the user intended. Instead, you’d have to add your own separate selector to the UI of you need that data or use what you get from the browser.
I think Google Calendar has a good example of a UI pattern for that purpose.