Displaying LocalDateTime in Grid

I’m trying to display a LocalDateTime vale in a Grid.

From what I understand, I need a converter to convert this to a java.util.Date value, and a DateRenderer. The trouble is, I can’t seem to do that.

If I do:

grid.addColumn("date").setConverter(new LocalDateTimeToDateConverter());

I get:

java.lang.IllegalArgumentException: The converter presentation type class java.util.Date is not compatible with the renderer presentation type class java.lang.String (in Column[propertyId:date]
)

But if I do:

grid.addColumn("date").setRenderer(new DateRenderer());

I get:

java.lang.IllegalArgumentException: Cannot remove converter, as renderer's presentation type java.util.Date and column's model java.time.LocalDateTime type aren't directly compatible with each other (in Column[propertyId:date]
)

It would seem I need to change/set both the converter and the renderer at once, but I don’t see how that’s possible?

If I don’t set the renderer or converter, the field displays OK as a string, but I can’t grid editor, as:

com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type java.time.LocalDateTime to presentation type class java.lang.String. No converter is set and the types are not compatible.

What I’d really like to do is have the in-line grid editor use a DateField for this column, but I don’t think that’s possible?

TL;DR: What’s the best way to display/edit LocalDateTime fields in a grid?

Hi!
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy ");
grid.addColumn(“date”, Date.class);
deviationGrid.getColumn(“date”).setRenderer(new DateRenderer(sdf));
I’m trying to do that in this way, but I’m not sure it’s the best one.

With Vaadin 8, you can employ addColumn(ValueProvider) to convert the LocalDateTime to String e.g. by calling toInstant().toString(); then call setId(“date”) on that column so that it will be picked by Editor’s Binder. Then you can use the DateTimeField component which in Vaadin 8 uses LocalDateTime.

However sometimes the columns are auto-generated (when using new Grid(MyBean.class) ). In that case the ValueProvider solution is not employable since the column is already there and one cannot change ValueProvider on an existing column. You can then use a custom Renderer which simply extends TextRenderer and overrides

public JsonValue encode(Object value) {
    return value == null ? super.encode(value) : Json.create(((LocalDateTime) value).toInstant().toString());
}

In Vaadin Framework 8.1 you’ll also have the option to give a separate presentation value provider for a Grid column. This should simplify things like having a DB date converted to an editable as a LocalDate but presented in non-editing mode as a string formatted the way you want to format it.

Vladimir Yarm:
Hi!
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy ");
grid.addColumn(“date”, Date.class);
deviationGrid.getColumn(“date”).setRenderer(new DateRenderer(sdf));
I’m trying to do that in this way, but I’m not sure it’s the best one.

can you explain this more detail?
what is deviationGrid? is it the name of your grid?
what grid in grid.addcolumn?
thank you

Adiva Hyrdie:

Vladimir Yarm:
Hi!
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy ");
grid.addColumn(“date”, Date.class);
deviationGrid.getColumn(“date”).setRenderer(new DateRenderer(sdf));
I’m trying to do that in this way, but I’m not sure it’s the best one.

can you explain this more detail?
what is deviationGrid? is it the name of your grid?
what grid in grid.addcolumn?
thank you

Sorry. deviationGrid is simple grid.I forgot to change it everywhere. This code should be like this

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy ");  
grid.addColumn("date", Date.class);  
grid.getColumn("date").setRenderer(new DateRenderer(sdf));  

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-mm-dd HH:mm”).withZone(ZoneId.systemDefault());

grid.addColumn(Object → formatter.format(Object.getCreationDate())).setCaption(“XXX”);